You are not logged in.
some more sharing, both of (maybe amateurish) bash scripting and of nonetheless functional and useful little scripts.
1. append
as the name implies, it appends stuff to filenames. 'append [string] [file]' or 'append [string] -d' for the entire directory.
#!/bin/bash
# custom script for appending stuff to filenames
# replaces spaces with underscores as well for posterity
# use 'append [string] [file]' or 'append [string] -d' to
# append to all files in directory
IFS='
'
if [ $# -lt 2 ]; then
echo "Use 'append [string] [file]' (single file) or 'append [string] -d' (all files in directory)."
exit 1
fi
if [ $2 == "-d" ]; then
for i in *; do
FILE=("${FILE[@]}" "$i")
done
else
if [ -e $2 ]; then
FILE=("${FILE[@]}" "$2")
else
echo "$2: No such file here."
exit 1
fi
fi
for ((x=0; x<${#FILE[*]}; x++)); do
curr=${FILE[x]}
mv -v "$curr" `echo $1$curr`
done
exit 02. firep
a nice Linux-style abbreviation of everyone's favorite, find-and-replace. this one is quite new and still under construction, but only to add more options (currently defaults to replace only once, and i want it optional for multiple replacements). same basic structure as above: 'firep [find_string] [replace_string] [file]' or 'firep [find_string] [replace_string] -d' for entire directory.
#!/bin/bash
# firep - a find-and-replace script
# use 'firep [find_string] [replace_string] [file]' or
# 'firep [find_string] [replace_string] -d' to
# find-and-replace on all files in directory
IFS='
'
if [ $# -lt 3 ]; then
echo "Use 'firep [find_string] [replace_string] [file]' (single file) or 'firep [find_string] [replace_string] -d' (all files in directory)."
exit 1
fi
if [ $3 == "-d" ]; then
for i in *; do
FILE=("${FILE[@]}" "$i")
done
else
if [ -e $3 ]; then
FILE=("${FILE[@]}" "$3")
else
echo "$3: No such file here."
exit 1
fi
fi
for ((x=0; x<${#FILE[*]}; x++)); do
curr=${FILE[x]}
firep=$(sed "s/$1/$2/" <<< $curr)
mv -v -- "$curr" $(echo $firep)
done
exit 0let me know what you think, if/when you find it useful and/or if/when you have some bash-script-tips or anything like that.
feel free to steal and change the code and everything. the only thing i ask is if you make changes or use some of my code in your own scripts, to please share them with the community as well. that way we can all keep each other educated and supplied with nice little scripts.
Last edited by rhowaldt (2012-01-08 21:12:45)
Offline
Hi rhowaldt,
Two remarks:
1. $# is always one more than the real number of arguments.
($0 is the command)
2. Error handling!
Example: If there is a problem with one of the files being moved:
the user should be notified in one way or another
can you continue with the next file or do you stop the script there?
And third of my 2 remarks:
Do you handle files with special characters in them correctly? (spaces, *'s....)
hth
bootinfoscript - emacs primer - I ♥ #!
Offline
@xaos52: thanks for replying, and with good points too!
1. i think this piece of code suffers from copy-paste-edit a bit. so i might've re-used and edited without thinking, or without counting.
2. error handling for your example is done through the -v (verbose) option for the 'mv' program, which gives feedback (damn, i initially wrote 'facebook' instead of 'feedback', can you believe it? i am so glad i deleted my account) on the move-process.
this might be insufficient but i'm having trouble imaging the different errors i might get.
3. there is no special character handling built-in, no. this (and some of no. 2 as well) stems from the fact that i scripted these first-and-foremost for personal use. i use these mainly for MP3-files, and i have a 'fclean' script which i run on the files first, which handles all the special characters etc. so because of that workflow i never thought of building this in these scripts as well.
i'll keep these in mind and change where i see fit. i do maintain these are primarily for personal use, so some things i might not change. i think it is good to address these things though, as anyone who is looking through here for scripting tips and ideas will (hopefully) read this as well. and might i ever take a stab at scripting something not primarily for personal use, i will have to remember these as well.
Last edited by rhowaldt (2012-01-08 13:53:43)
Offline
Just for the record, there is a utility in the repos called rpl.
Description: intelligent recursive search/replace utility
rpl is a text replacement utility. It will replace strings with new strings
in multiple text files. It can work recursively over directories and supports
limiting the search to specific file suffixes.
I know you enjoy hacking these things together, as I do, but before you invest many many hours in this particular project...
John
--------------------
( a boring Japan blog , and idle twitterings )
Offline
^ thanks for the info! did not know about that. was hard to imagine someone hadn't thought of this before but couldn't find it at the time. i have already invested the time and the script is done and i use it regularly and with great success. in fact, i love my hacked-together script 
Offline
Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.