Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management.

Similar presentations


Presentation on theme: "UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management."— Presentation transcript:

1 UNIX-21 WEEK 2 4/5/2005

2 UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management

3 UNIX-23 Sample Files Download states.txt and statecap.txt from acelinux.tripod.com

4 UNIX-24 cut Used to extract fields from a line By defaults, fields are delimited by tab Syntax: cut –d -f e.g. cut –d ‘:’ –f1 /etc/passwd displays 1 st column of file /etc/passwd cut –d ‘:’ –f2 /etc/passwd displays 2 nd column of file /etc/passwd

5 UNIX-25 cut (contd.) more /etc/passwd cut –d’:’ -f1 /etc/passwd > usernames more usernames cut –d’:’ –f1-3 /etc/passwd cut –d’ ‘ –f2 statecap.txt cut –d’ ‘ –f2 statecap.txt > capcities

6 UNIX-26 Examples of cut

7 UNIX-27 Exercise Write a shell script that displays the current date and time on separate lines `

8 UNIX-28 dt=`date` day=`echo $dt | cut –d’ ‘ –f1` echo Day is $day mnth=`echo $dt | cut –d’ ‘ –f2` echo Month is $mnth dte=`echo $dt | cut –d’ ‘ –f3` echo date is $dte tme=`echo $dt | cut –d’ ‘ –f4` echo time is $tme yr=`echo $dt | cut –d’ ‘ –f6’ echo year is $yr

9 UNIX-29 day=`date | cut –d’ ‘ –f1` echo Day is $day mnth=`date | cut –d’ ‘ –f2` echo Month is $mnth dte=`date | cut –d’ ‘ –f3` echo date is $dte tme=`date | cut –d’ ‘ –f4` echo time is $tme yr=`date | cut –d’ ‘ –f6’ echo year is $yr

10 UNIX-210 paste Command to combine files. By default, adds a space between fields e.g. paste /etc/passwd /etc/passwd paste –d “X” /etc/passwd /etc/passwd

11 UNIX-211 paste - examples more states.txt paste states.txt states.txt > 2states.txt more 2states.txt paste states.txt /etc/passwd > junk more junk

12 UNIX-212 wc wc – word count Displays the number of lines, words and characters in the file(s) Syntax: wc filename e.g wc states.txt

13 UNIX-213 wc Options wc –l states.txt displays # of lines wc -m /etc/passwd displays # of characters wc –w states.txt displays # of words

14 UNIX-214 grep bash /etc/passwd | wc –l # displays # of users with bash as default shell grep csh /etc/passwd | wc –l # displays # of users with csh as default shell

15 UNIX-215 export Used to make variables available to all child shell processes By default, shell variables are available only the shell that they are defined in To make shell variables available in child shell processes, they have to be “exported”

16 UNIX-216 mainscript A in childscript is echo A in childscript is $A A=apple # define a variable A childscript # call another script. This script # executes in a child shell process mainscript childscript

17 UNIX-217 mainscript A in childscript is apple echo A in childscript is $A export A=apple # define a variable A childscript # call another script. This script # executes in a child shell process mainscript childscript

18 UNIX-218 source,. (dot command) source,. are used to execute scripts in the same shell. (dot) is more portable than source i.e. available on almost all versions of all shells Can be used from command line or from inside a script

19 UNIX-219. (cmd-line) When invoked on command line, executes the script. myscript Executes myscript in the current shell

20 UNIX-220 county=fairfax myscript. myscript or source myscript echo $county fairfax echo $county

21 UNIX-221 export city=centreville myscript2./myscript2 echo $city

22 UNIX-222 echo Setting county variable county=fairfax myscript. myscript or source myscript echo $county fairfax echo $county Setting county variable

23 UNIX-223. (within script) When used within a script,. executes the called script in the same shell as the main script Same effect as if “sourced” script was imported into the main script

24 UNIX-224 echo Setting county variable county=montgomery myscript echo executing myscript myscript echo Value of county is $county mymainscript executing myscript Setting county variable Value of county is

25 UNIX-225 echo Setting county variable county=montgomery myscript echo executing myscript. myscript echo Value of county is $county mymainscript executing myscript Setting county variable Value of county is montgomery

26 UNIX-226 Importing functions Functions can be imported into a script using the. command Once imported, functions can be used in script as if they were defined in the script. Need to import the complete function file Functions are NOT executed when imported – only when “called”

27 UNIX-227 sum_fn() { let result=$1+$2 return $result } diff_fn() { let result=$1-$2 return $result } funfile

28 UNIX-228. funfile sum_fn $1 $2 echo sum of $1 and $2 is $? diff_fn $1 $2 echo difference of $1 and $2 is $? mathscript mathscript 10 30 sum of 10 and 30 is 40 difference of 10 and 30 is -20

29 UNIX-229 Exercise Rewrite the calculator script by writing all the functions in a single file and “importing” that file into the main script

30 UNIX-230 pushd, popd, dirs Set of commands to “bookmark” working directories Traverse directories in order First In Last Out pushd dir-name pushes dir-name to directory stack and changes current directory to dir-name popdremoves top directory in stack and changes current dir to the dir popped from stack

31 UNIX-231 pushd, popd, dirs dirs lists contents of directory stack pushd, popd commands also list contents of directory stack Used in scripts that require changes to current-working directory without hard- coding directory name

32 UNIX-232 pushd - examples pushd /tmp pwd pushd /usr pwd dirs pushd /bin pwd dirs

33 UNIX-233 popd - examples pwd popd dirs pwd popd pwd dirs popd pwd

34 UNIX-234 pushd, popd in scripts # script pushd somedirectory # we are now in somedirectory … popd # back in original directory …

35 UNIX-235 Shell Debugging No debugger nor any debugging commands/constructs Cryptic error messages Use bash options for primitive debugging

36 UNIX-236 Bash debugging options -v echoes each command before executing it (before evaluating) -n checks for syntax errors without actually executing the script (weak check) -x print each command before executing (after evaluating)

37 UNIX-237 Debugging method-1 Add option to bash definition in 1 st line of script #!/bin/bash –v …

38 UNIX-238 #!/bin/bash –x echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi

39 UNIX-239 #!/bin/bash –v echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi

40 UNIX-240 #!/bin/bash –n echo Enter password read pass if[ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi

41 UNIX-241 Debugging Method - 2 Execute script in cmd-line bash with required option Don’t need to hard-code option inside script $ bash –v myscript

42 UNIX-242 #!/bin/bash echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi $ bash –v myscript myscript

43 UNIX-243 #!/bin/bash echo Enter password read pass if [ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi $ bash –x myscript myscript

44 UNIX-244 #!/bin/bash echo Enter password read pass if[ “$pass” != “magic” ] then echo Wrong password else echo Correct password fi $ bash –n myscript myscript

45 UNIX-245 Downloading bash 3.0 Create a temporary directory /tmp/bash mkdir /tmp/bash Go to: http://ftp.gnu.org/gnu/bash/ File: bash-3.0-tar.gz Download file to /tmp/bash

46 UNIX-246 Downloading bash-3.0 (contd.) cd /tmp/bash cd to directory gunzip bash-3.0.tar.gz Unzip file tar –xvf bash-3.0.tar Un-tar the file cd bash-3.0./configure Run configure script make Run make utility to Build bash binary ls –l bash Verify bash was built successfully cp bash ~ Copy bash binary to home dir

47 UNIX-247 read (re-visited v3.0+) read command can be used to read multiple variables -s do not echo input -n N accept only N chars of input -p string echo string as prompt before reading input

48 UNIX-248 read –s –n 6 –p “Enter password” pass accepts 6 char input, does not echo keystrokes and prompt is “Enter password” read –p “Enter 2 numbers” x y accepts 2 values into x and y. If less values are passed, remaining variables are not set

49 UNIX-249 #!/home/acestudent7/bash read –s –n 6 –p “Enter password” pass

50 UNIX-250 Exercise Write a script that prompts the user to create a new 6 character password. The user must be prompted to reconfirm the new password. A success message must be displayed if the user re-enters the password correctly else a failure message must be displayed. The password must not be echoed on the screen.

51 UNIX-251 Enter 6 char password Re-enter password Password entered successfully Enter 6 char password Re-enter password Passwords do not match Sample execution of script

52 UNIX-252 dt=`date` day=`echo $dt | cut –d’ ‘ –f1` mnth=`echo $dt | cut –d’ ‘ –f2` dte=`echo $dt | cut –d’ ‘ –f3` tme=`echo $dt | cut –d’ ‘ –f4` yr=`echo $dt | cut –d’ ‘ –f6’ bkupdir=/tmp/backup_$day_$month_$tme_yr mkdir $bkupdir cp –R ~ $bkupdir

53 UNIX-253 Exercise Write a shell script that does the following: Creates a directory called backup_ in the /tmp/directory Copies all files and directories in your home directory to the new directory

54 UNIX-254 Scheduling Jobs - cron cron – utility used to execute commands at specified times/dates Output/error of commands is e-mailed to user Commands are executed with privileges of user who setup the cron-job

55 UNIX-255 crontab Command to schedule jobs Each input line corresponds to a job Each input line must contain 6 space separated fields 1-5 date/time 6 – command to be executed

56 UNIX-256 crontab options -l list user’s crontab tasks -r remove all of user’s crontab tasks -e edit user’s crontab file

57 UNIX-257 crontab time/date fields 1.Minute (0 – 59) 2.Hour (0 – 23) 3.Day-of-month (1 – 31) 4.Month-of-year (1 – 12) 5.Day-of-week (0 – 6) (0=Sunday) *  all valid values Comma-separated values Specify range using hyphen 6. Command to be executed

58 UNIX-258 crontab - examples 00 05 * * 1,2,3,4,5 script1 arg1 5am M-F 45 06 * * 5 somescript 6:45am every Fri 00 01 1 * * myscript 1am 1 st of every month

59 UNIX-259 crontab – setting up jobs crontab * * * * * /home/acestudent7/mybackupscript Ctrl-d

60 UNIX-260 crontab – setting up jobs (contd.) crontab – eallows to setup jobs in a editor EDITOR variable must be set to a valid editor export EDITOR=gedit

61 UNIX-261 crontab – viewing jobs crontab –l displays current job list

62 UNIX-262 jobs List jobs running in the background sleep 1000 & jobs kill %1 stops the first job kill 1932stops job with id 1932

63 UNIX-263 wait Stops script execution until all jobs running in background have terminated Or until job with number specified terminates … wait 200 # wait till job with process id 200 terminates …

64 UNIX-264 $! Process id of last back-ground process find / -name core –print 2> /tmp/err > out & pid=$! … wait $pid mail user < out

65 UNIX-265 Next Week Application Development Procedures Compilers and GNU development tools Version control demo


Download ppt "UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management."

Similar presentations


Ads by Google