Presentation is loading. Please wait.

Presentation is loading. Please wait.

Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)

Similar presentations


Presentation on theme: "Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)"— Presentation transcript:

1 Various 2

2 readonly readonly x=4 x=44 #this will give an error (like what in java?)

3 talking to myself echo echo talking to myself > newScript.sh sh newScript.sh rm newScript.sh #what are the potential problems with this? #how can it be improved?

4 Command substitution the shell can insert standard output of a command at any point in the command line. There are two ways to do this `…` backquotes $(…)

5 Example - Command substitution echo date is `date` Notice the backquotes! echo date is $(date) d=$(date) echo date is $d [zlizjw1@unnc-cslinux ~]$ d=`date` [zlizjw1@unnc-cslinux ~]$ echo $d Tue Apr 7 15:52:35 CST 2009

6 $(…) or `…` $() is better that `` easier to read! (think of if in java) can nest easier $($(...)) note can nest with backquotes too

7 quotes '...' removes meaning of all enclosed characters "..." removes meaning of all enclosed characters except $, ` and \ \c removes meaning of special character c ($, ', " newline, and \) `command` or $(command) executes the command (\ is also used for continuing new lines)

8 A unix philosophy If you have to do it more than twice – write a script to do it. This will take time at the beginning – but you will become faster. The overhead is worth it. If you have to do it more than twice – so has someone else – so you can google for the code

9 What is the error here? for i in 1 2 3 do echo i done

10 No Error!! It may be what the person wanted. You need to explain what you require. You can write what you want the expected output to be. Or explain.

11 \ for continuing new lines $ ls | wc 20 20 277 Or if it is a long command I can continue it on another line ls \ ? | wc 20 20 277 # a better example would be with awk or grep.

12 PS2 is the prompt here $ PS2=p2 $ echo $PS2 p2 $ ls \ p2 | wc 19 19 264 $ Note – this is useful for long chains of commands.

13 Debugging (tracing) sh -x prog.sh or can do as set -x or can switch on and off in the script turn off with set +x (note that this is counter intuitive!!!) set with no arguments shows the variables. Printing is a good idea to confirm the values of variables, and isolate problems.

14 Setting a variable to null var= var="" var='‘ #two single quotes. var=`` #two back quotes are these all the same? If not – what are the error messages. Try it out for yourself.

15 Single and Double Quotes Single quote protect everything echo '$(ls)’ #will print...what? Double quotes filelist=$(ls) echo $filelist echo "$filelist" good for storing contents of a file namelist=$(cat names.txt) (we can do text processing on this)

16 expr expr 1 + 2 expr 1 * 2 (this will give an error – what error?) expr 1 \* 2

17 ${parameter} vs $parameter mv $file $fileBackUp (we want to append the filename) mv ${file} ${file}x Also for arguments to a script. This can also be used for ${1} cannot say $11 need ${11}

18 Null command : if true then : #we have to put something here else echo false fi #this is good when writing programs.

19 (…) and {…;} (..) this works in a subshell {…;} works in the current shell x=10 (x=40) echo $x # what will this print? {x=70;} echo $x #what will this print? #see subshell.sh on shared drive.

20 (…) example (command1; command 2 ) & This will do command1, then command2, but the parent shell can continue working. This is not the same as (command1 &; command2 & ) & command1; command 2 & Why not? What do these do?

21 {…} example {command1; command2; } 2> errors.txt Here the error output of the two commands is sent to the file errors.txt Is this the same as command1 2> errors.txt; command2 2> errors.txt { sh thisFileDoesNotExist.sh ; sh OrThisOne.sh ;} 2> errors.txt #see share groupingCommands.sh

22 break for i in 1 2 3 do echo $i [ $i -eq 2 ] && break Done #break.sh on share

23 Break n #this will break out of the loop for i in 1 2 3 do echo $i [ $i -eq 2 ] && break $n done This will break out of the n innermost loops #breakN.sh on share

24 continue #this will continue with the the loop for i in 1 2 3 do echo before $i [ $i -eq 2 ] && echo skip && continue echo after $i done

25 Continue n Just like break has a break n Continue has a continue n See continue.sh and continueN.sh on share.

26 Loop redirection You can perform redirection on a loop for ((i = 1; i < 5; i++)) do echo $i done > out.txt #on share at loopRedirection.sh

27 Loop in background #this will execute a loop in the background. for ((i = 1; i < 50000; i++)) do echo $i >> out.txt done &

28 loopPiping #the output of the loop is piped into wc for ((i = 1; i < 5; i++)) do echo $i done | wc #see loopPiping.sh on share.

29 Forcing output 1 echo hello This prints to the screen (tellytype) by default OR whereever you direct it echo hello > out.txt

30 Forcing output 2 #If we want to force the output to the screen in a script #try to run this program as #sh devtty.sh > out.txt echo this will always go to the screen > /dev/tty

31 Be careful with rm (rm -i) $ ls a.txt b.txt c.txt remove.sh remove.sh~ $ rm a.txt rm: remove regular empty file `a.txt'? y $ sh remove.sh $ ls remove.sh remove.sh~


Download ppt "Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)"

Similar presentations


Ads by Google