Topic: two simple scripts: append & firep
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)