basicsys@mars~/lec9>cat A3 #!/bin/bash x=8 function f1 { echo "inside f1 x=$x" x=9 y=10 } echo "outside before calling f1 x=$x y=$y" f1 echo "outside after calling f1 x=$x y=$y" basicsys@mars~/lec9>A3 outside before calling f1 x=8 y= inside f1 x=8 outside after calling f1 x=9 y=10 basicsys@mars~/lec9>cat A4 #!/bin/bash x=8 function f1 { echo "inside f1 x=$x" local x local y=10 echo "inside f1 after local statement x=$x y x=9 } echo "outside before calling f1 x=$x y=$y" f1 echo "outside after calling f1 x=$x y=$y" basicsys@mars~/lec9>A4 outside before calling f1 x=8 y= inside f1 x=8 inside f1 after local statement x= y=10 outside after calling f1 x=8 y= basicsys@mars~/lec9>cat P1 #!/bin/bash echo $1 f1() { echo $1 for x in "$@" do echo "$x" done } f1 f1 "20 30" 40 echo $1 basicsys@mars~/lec9>P1 400 5 400 20 30 20 30 40 400 basicsys@mars~/lec9>cat P1 #!/bin/bash f1() { echo $[$1**2 + $2**2] y=$[$1**2 + $2**2] } f1 2 3 echo y=$y x=$(f1 3 4) echo x=$x echo y=$y echo "$(f1 6 7)" echo y=$y f1 8 9 echo y=$y basicsys@mars~/lec9>P1 13 y=13 x=25 y=13 85 y=13 145 y=145 basicsys@mars~/lec9>cat P2 #!/bin/bash f1() { echo I am f1 in P2 } f1 basicsys@mars~/lec9>cat f1 #!/bin/bash echo "I am file f1" basicsys@mars~/lec9>P21 I am f1 in P2 I am file f1 I am f1 in P2 basicsys@mars~/lec9>cat B1 #!/bin/bash get_file_name(){ echo $1 | tr "/" "\n" | tail -1 } get_file_name a/b/c #find $1 -type f -exec get_file_name {} \; basicsys@mars~/lec9>chmod u+x B1 basicsys@mars~/lec9>B1 c basicsys@mars~/lec9>cat B1 #!/bin/bash get_file_name(){ echo $1 | tr "/" "\n" | tail -1 } get_file_name a/b/c find $1 -type f -exec get_file_name {} \; basicsys@mars~/lec9>B1 . c find: get_file_name: No such file or directory find: get_file_name: No such file or directory find: get_file_name: No such file or directory basicsys@mars~/lec9>cat B1 #!/bin/bash get_file_name(){ echo $1 | tr "/" "\n" | tail -1 } get_file_name a/b/c export -f get_file_name find $1 -type f -exec bash -c "get_file_name {}" \; basicsys@mars~/lec9>B1 . c f2 A3 basicsys@mars~/lec9>cat C1 #!/bin/bash echo -n >| FF for i in $(seq 10) do echo F$i >> FF done basicsys@mars~/lec9>cat FF F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 basicsys@mars~/lec9>cat F[0-9]* I am file F1 I am file F10 I am file F2 I am file F3 I am file F4 I am file F5 I am file F6 I am file F7 I am file F8 I am file F9 basicsys@mars~/lec9>ls F[0-9]* F1 F10 F2 F3 F4 F5 F6 F7 F8 F9 basicsys@mars~/lec9>ls F[0-9]* | xargs cat I am file F1 I am file F10 I am file F2 I am file F3 I am file F4 I am file F5 I am file F6 I am file F7 I am file F8 I am file F9 basicsys@mars~/lec9>xargs cat F1 F5 F7 F8 F2 I am file F1 I am file F5 I am file F7 I am file F8 I am file F2 basicsys@mars~/lec9>xargs echo abc def ghe abc def ghe basicsys@mars~/lec9>xargs echo -n abc def abc defbasicsys@mars~/lec9> basicsys@mars~/lec9>echo 1 2 3 4 5 6 7 8 9 |xargs -n 3 echo 1 2 3 4 5 6 7 8 9 basicsys@mars~/lec9>echo 1 2 3 4 5 6 7 8 9 10| xargs -n 3 echo 1 2 3 4 5 6 7 8 9 10 basicsys@mars~/lec9>cat G2 #!/bin/bash echo "using axargs" date find ~/win12 -type f | xargs egrep -l abc >|/dev/null date echo "using -exec " date find ~/win12 -type f -exec egrep -l abc {} \; >|/dev/null date basicsys@mars~/lec9>G2 using axargs Fri Jan 6 13:20:03 IST 2012 Fri Jan 6 13:20:03 IST 2012 using -exec Fri Jan 6 13:20:03 IST 2012 Fri Jan 6 13:20:26 IST 2012 basicsys@mars~/lec9>x=abcde basicsys@mars~/lec9>echo $x abcde basicsys@mars~/lec9>echo ${x} abcde basicsys@mars~/lec9>xx=5 basicsys@mars~/lec9>echo ${x}x abcdex basicsys@mars~/lec9>echo ${xx} 5 basicsys@mars~/lec9>echo $xx 5 basicsys@mars~/lec9>x=abcdef basicsys@mars~/lec9>echo ${#x} 6 basicsys@mars~/lec9>expr length $x 6 basicsys@mars~/lec9>echo -n $x | wc -c 6 basicsys@mars~/lec9>echo ${#abcdef} 0 basicsys@mars~/lec9>expr match abcdef ab.[cd] 4 basicsys@mars~/lec9>expr match abcdef a*.[cd] 3 basicsys@mars~/lec9>expr match abcdef '\('a*.[cd]'\)' abc