UNIX TRAINING MODULE Shell- Scripti ng
Learning Objectives 01- 02- 03- 04- Understandin Introduction Cognizance to Performing Applied Shell g what is a to Bash Running Programming Shell and Commands Shell Script
INTRODUCTION TO UNIX • A Unix shell is a command-line interpreter or shell that SHELL gives Unix-like operating systems a command line user interface. • The operating system uses the shell, which is both an interactive command language and a scripting language, to manage how system scripts are executed. • A programme known as a shell is one that runs other programmes in response to text instructions. • By passing named variables, a parameter list, or an input source, a smart shell can also alter the circumstances under which other programmes run. • Users frequently have a wide variety of command-line interpreters to choose from in Unix-like operating systems for interactive sessions. • An automated shell programme is launched for the length of the user session when they log in interactively. • The user's profile normally stores the kind of shell, which may be customised for each user, for example in the local passwd file or in a distributed configuration system like NIS or LDAP. • However, the user is free to interactively execute any other accessible shell.
L ROC ESS
TYPES OF SHELL Bourne Shells • The Bourne shell is the original Unix shell -- • It provides command-based programming to interpret command execution program, often called a command and execute user commands. interpreter. • As a user types a command, the shell interprets it so • It was developed in 1979 at what at the time was Bell the operating system can take action, such as Labs. Named for its developer, English computer automating a task. scientist Stephen Bourne, the Bourne shell is also known by its program name, sh. The Bourne shell • The Bourne shell was the default shell for Unix family also includes the Korn shell, bash (Bourne version 7. Again Shell) and the Z shell (zsh). • The shell prompt used in command prompts is the $ • The Bourne shell is used for scripting. symbol. • /bin/sh • /bin/bash?“Bourne-Again Shell”
WHAT DO I MEAN BY /BIN ? • C Shell (/bin/csh) • Turbo C Shell (/bin/tcsh) • Korn Shell (/bin/ksh) • /bin, /usr/bin, /usr/local/bin • /sbin, /usr/sbin, /usr/local/sbin • /tmp • /dev • /home/borwicjh
INTRODUCTION TO SHELL SCRIPTING • A shell script is a list of commands in a computer program that is run by the Unix shell which is a command line interpreter. • A shell script usually has comments that describe the steps. • The different operations performed by shell scripts are program execution, file manipulation and text printing. • A wrapper is also a kind of shell script that creates the program environment, runs the program etc.
SHELL SCRIPT Hello, world A TEXT FILE
INPUT SHELL SCRIPT Redirection ENV OUTPUT ERROR cat > /tmp/myfile cat >> /tmp/myfile cat 2> /tmp/myerr cat < /tmp/myinput cat <<INPUT?Some input?INPUT cat > /tmp/x 2>&1
SHELL SCRIPTING Run #!/bin/sh What echo ‘Hello, world’ To Do MY_PROGRAM Executa ble % %cacth>mhoedllo+.xsh Runnin <<MhYe_lPloR.OshG%RAM g it .#/h!/eblilno/.sh echHoe‘Hlloe,llwo,owrlodrld’ MY_PROGRAM % chmod +x % cahte>llhoe.slhlo.sh <<M%Y._/hPeRlOloG.sRhAM H#e!l/lboi,nw/oshrld echo ‘Hello, world’ MY_PROGRAM % chmod +x % cat > hello.sh hello.sh <<MY_PROGRAM %#.!//hbeinll/os.hsh echHoe‘Hlloe,llwo,owrlodrld’ MY_PROGRAM % chmod +x
Finding the program: PATH % ./hello.sh echo vs. /usr/bin/echo % echo $PATH?/bin:/usr/bin:/usr/local/bin:?/ home/borwicjh/bin % which echo?/usr/bin/echo
VARIABLES AND THE ENVIRONMENT % hello.sh bash: hello.sh: Command not found % PATH=“$PATH:.” % hello.sh Hello, world % env […variables passed to sub-programs…] % NEW_VAR=“Yes” % echo $NEW_VAR Yes % env […PATH but not NEW_VAR…] % export NEW_VAR % env […PATH and NEW_VAR…]
Quoting % echo ‘$USER’ $USER % echo “$USER” borwicjh % echo “\\”” ” % echo “deacnet\\\\sct” deacnet\\sct % echo ‘\\”’ \\”
How to Learn => man • man bash • man cat • man man => man –k • man –k manual => Learning the Bash Shell, 2nd Ed. => “Bash Reference” Cards => http://www.tldp.org/LDP/abs/html/
Continuing Lines: \\ % echo This \\ Is \\ A\\ Very \\ Long \\ Command Line This Is A Very Long Command Line %
Exit Status and Exit $? % cat > test.sh <<_TEST_ 0 is True exit 3 % ls /does/not/exist _TEST_ % echo $? % chmod +x test.sh 1 % echo $? % ./test.sh 0 % echo $? 3
% test 1 -lt 10 test- % echo $? [ ]- 0 % test 1 == 10 [ 1 –lt 10 ] % echo $? [[ ]]- 1 [[ “this string” =~ “this” ]] (( ))- (( 1 < 10 )) Logic: Test • [ -f /etc/passwd ] • [ ! –f /etc/passwd ] • [ -f /etc/passwd –a –f /etc/shadow ] • [ -f /etc/passwd –o –f /etc/shadow ]
$(( )) for Math % echo $(( 1 + 2 )) 3 % echo $(( 2 * 3 )) 6 % echo $(( 1 / 3 )) 0
if something Logic: IF if [ $USER –eq “borwicjh” ] then then : : # “elif” a contraction of “else if”: elif something-else # “elif” a contraction of “else if”: then elif ls /etc/oratab : then else : then else : then fi : fi # see if a file exists if [ -e /etc/passwd ] then echo “/etc/passwd exists” else echo “/etc/passwd not found!” fi
for i in 1 2 3 for i in /* do do echo $i echo “Listing $i:” done ls -l $i read done Logic: FOR for i in /* for i in /* do do echo “Listing $i:” echo “Listing $i:” ls -l $i ls -l $i read read done done
for (( expr1 ; LIMIT=10 expr2 ; for (( a=1 ; expr3 )) do a<=LIMIT ; list a++ )) done do echo –n “$a ” done Logic: C-STYLE FOR
Logic: WHILE while something a=0; LIMIT=10 do while [ \"$a\" -lt \"$LIMIT\" ] : do done echo -n \"$a ” a=$(( a + 1 )) done
Coun COUNTER=0 ters while [ -e “$FILE.COUNTER” ] do COUNTER=$(( COUNTER + 1)) done Note: race condition
Reusing Code: “Sourcing” % cat > /path/to/my/passwords <<_PW_ FTP_USER=“sct” _PW_ % echo $FTP_USER % . /path/to/my/passwords % echo $FTP_USER sct %
le Manip ulat%io FILEPATH=/path/to/my/output.lis n % echo $FILEPATH /path/to/my/output.lis % echo ${FILEPATH%.lis} /path/to/my/output % echo ${FILEPATH#*/} path/to/my/output.lis % echo ${FILEPATH##*/} output.lis
Email % echo “Message” | \\ Notific mail –s “Here’s your ation message” \\ [email protected]
Date % DATESTRING=`date +%Y%m%d` s % echo $DATESTRING 20060125 % man date
Hard Way FTP ftp –n –u server.wfu.edu wget curl <<_FTP_ user username password • wget curl –T upload-file \\ put FILE \\?ftp://user:pass@serv -u username:password \\ _FTP_ er.wfu.edu/file ftp://server.wfu.edu/dir/file • wget –r \\?ftp://user:pass@serv er.wfu.edu/dir/
Searc % find Locate Grep /home/borwicjh hing \\ % locate .lis % grep rayra [files with .lis /etc/passwd -name ‘*.lis’ in path] % grep –r [all files % locate log rayra /etc matching *.lis] [also finds % grep –r % find “/var/log/mes RAYRA /etc /home/borwicjh sages”] % grep –ri \\ RAYRA /etc % grep –rli -mtime -1 – rayra /etc name ‘*.lis’ [*.lis, if modified within 24h] % man find
Monitoring Processes • ps • ps –ef • ps –u oracle • ps –C sshd • man ps
Search
Read the Text Version
- 1 - 31
Pages: