banner
Dave Horner's Website - Yet another perspective on things...
Home Tech Talk Programming Bash and batch programming
If you appreciate the information found on this website, please drop me a line!

Who's Online

We have 21 guests online
Content View Hits : 1157105
moon and stars
How did you find my site?
 
How often do you answer random online questions?
 

Random Quote

"[Programmers] are attached to their programs. Indeed, their programs become extensions of themselves - a fact which is verified in the abominable practice of attaching one's name to the program itself..." --Gerald M. Weinberg, The Psychology of Computer Programming
Party_Pics_Halloween_2007_009
P1010026
Zoo_2007_050
DSC00379

Bash and batch programming

Saturday, 26 March 2005 22:08

Wanna loop over files in bash and perform a cmd on those files?

# (where $i is replaced by the name of the file)
for i in ./*.conf; do cmd -c $i; done

or using find
# (where {} is replaced by the name of the file)
find . -name *.conf -exec cmd {} \;
I usually opt. for the find version because it seems to be more capable.

Then if you are on windows, you don't get find and you can't use the bash syntax of course.... So use, forfiles! What a gem. Gives you access to filename without extension and do things differently if you are testing for files or folders.
forfiles is very powerful.
O'Reilly -- Ten Essential Windows 2000 Commands


Cutting up file/path strings in bash

Of course you could use the programs 'dirname' or 'basename'...but you can also do it using bash regex...
Get directory portion of path
dirportion = ${path%/*}
Get the filename of path
filename = ${path##*/}
Get the basename of the file (without extension)
basename = ${path%%.*}
Get the extension of the path
ext = ${file#*.}
These examples are based on these operations:
${variable%pattern}
Trim the shortest match from the end
${variable##pattern}
Trim the longest match from the beginning
${variable%%pattern}
Trim the longest match from the end
${variable#pattern}
Trim the shortest match from the beginning
bash String Manipulations Issue 18


Wanna make a CGI script in bash?

If you ever need to quickly code up a bash CGI script, use bashlib.

Great programming info:
Advanced Bash-Scripting Guide
Quick syntax guide
If
if [ expression ]
then
   commands
elif [ expression2 ]
then
   commands
else
   commands
fi
While
#This program lists the parameters that were passed to the program, along with the parameter number.
 
count=1
while [ -n "$*" ]
do
   echo "This is parameter number $count $1"
   shift
   count='expr $count + 1'
done
Case statement
case $1 in
-i)
   count='grep ^i $2 | wc -l'
   echo "The number of lines in $2 that start with an i is $count"
   ;;
-e)
   count='grep ^e $2 | wc -l'
   echo "The number of lines in $2 that start with an e is $count"
   ;;
*)
   echo "That option is not recognized"
   ;;
esac
For
# In this form, the for statement executes once for each item in the list. 
# This list can be a variable that contains several words separated by spaces, 
# or it can be a list of values that is typed directly into the statement. Each 
# time through the loop, the variable var1 is assigned the current item in the 
# list, until the last one is reached.
for var1 in list
do
   commands
done
How to run a command for each line of a file using sed.
sed is your friend.
for i in `sed -e 's/,.*$//g' < upgrade/4.1.9-4.1.11/file.lst`; do chmod 666 $i ; done


Find and replace using find and sed

find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;
Last Updated on Tuesday, 01 December 2009 14:01