Overview (ch 8) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng Admin determines which shell you use bash is the default shell in most of Linux systems /bin/bash Shell start-up /etc/profile (for all users) ~/.bash_profile or ~/bash_login ~/.bashrc and /etc/bashrc /etc/profile, ~/.bash_profile ~/.bashrc /etc/bashrc 1 2 echo this is y > y cat y cat x cat x y > log cat x y tr [a-z] [A-Z] 3 4 File descriptor 0: std input, 1: std output, 2: std error cat x y 1> log 2> err cat err Combine std input and std error (&>) cat x y &> log 5 6 1
Duplicate a file descriptor (>&) Left side: original channel Right side: current channel cat x y 1> log 2>&1 Duplicate a file descriptor (>&) Left side: original channel Right side: current channel cat x y 1>log 2>log cat x y 2>&1 1> log cat y x 1>log 2>log 7 8 Duplicate a file descriptor (>&) Left side: original channel Right side: current channel cat x y 1>&2 cat x y 2>err 1>&2 First script cat > whoson date echo Current Users: who CTRL+D cat x y 1>&2 tr [a-z] [A-Z] 9 10 Execute script chmod a+x whoson./whoson Execute script chmod a+x whoson./whoson Subshell takes commands from the script file chmod a-x whoson bash whoson chmod a-r whoson bash whoson 11 12 2
Environmental variables echo $PATH Search directories in order cd ~ whoson PATH=$PATH:~/it244 whoson Specify a shell in scripts (#!) cat >dash_script #!/bin/dash ps $$ ^D./dash_script Comments in script (#) 13 14 Separate and Group Commands Separation ( ; \ ) echo start; ls; echo end echo hello \ (newline) world echo hello \\ (newline) Separate and Group Commands Separation ( ; \ ) Within quotes echo Please choose: (newline) 1. yes (newline) 2. no echo Please choose: \(newline) 1. yes \(newline) 2. no 15 16 Separate and Group Commands Separation ( & ) Pipe ls l grep hello less Background jobs tail f log & cat > log Separate and Group Commands Group ( ) (ls -l; tail -f log; ls)& fg %1 CTRL+C 17 18 3
Job Control Suspend, fore/background ls& exetest& exetest& tail f log& jobs + is the last job, - is the previous job Letters, digits, and underscores Start with letters or underscores Valid: A123, _X_, MY_PATH Invalid: 123_ABC, MY-PATH, A&B 19 20 MYNAME=bo export MYNAME=bo echo $MYNAME MYNAME= bo sheng MYNAME= bo sheng MYNAME=bo\ sheng echo $MYNAME echo $MYNAME echo \$MYNAME 21 22 CMD=echo $CMD $MYNAME $CMD my name is $MYNAME. CMD= echo hello world $CMD CMD= echo $MYNAME $CMD Pathname expansion HELLO=hello* echo $HELLO echo $HELLO echo hello* 23 24 4
Braces { } PR=IT echo $PR244 $PR110 echo ${PR}244 $PR{110} PR=CS echo ${PR}244 $PR{110} Variable assignment in command lines (MYNAME=sheng; echo $MYNAME) echo $MYNAME echo echo $MYNAME > my_script MYNAME=sheng./my_script 25 26 Variable domain./my_script export MYNAME=bo./my_script Remove a variable person= echo $person unset person 27 28 Variable Attributes readonly variables person=zach echo $person readonly person person=helen readonly Variable Attributes declare and typeset Attribute Meaning -a An array -f A function -i An integer -r readonly -x Export (global variable) declare person1=max declare r person2=zach 29 30 5
Variable Attributes declare and typeset Attribute Meaning -a An array -f A function -i An integer -r readonly -x Export (global variable) declare rx person3=helen declare x person4 Variable Attributes declare and typeset Attribute Meaning -a An array -f A function -i An integer -r readonly -x Export (global variable) declare i COUNT 31 32 Variable Attributes List/remove attributes declare -r declare -x Processes What s a process Execution of a command Shell is a process declare +x person4 declare -x declare +r person2 fork() Two identical processes, but one is the parent and the other is the child. 33 34 Processes Execute a command shell shell fork execute the command fork sleep execute the command (background) PID and PPID sleep 10& ps -f Processes Builtins Variables cat > my_script declare -r sleep 10./my_script & ps -f 35 36 6
Keyword Variables PATH HOME HOSTNAME PS1/PS2 PS1=* PS1=$HOSTNAME PS1= \d \h \u - (table in Pg 310) PS2= --- Variables HISTSIZE HISTFILE HISTFILESIZE History cat ~/.bash_history history less history tail 37 38 History expansion!! Variable expansion echo $person echo $person echo $person Tilde expansion echo ~/it244 echo ~shengbo echo ~+ echo ~- 39 40 Pathname/filename expansion echo colors* echo zzz* echo colors* $person echo colors* $person var=colors* echo $var echo $var echo $var Command substitution $(command) echo $(pwd) mypwd=$(pwd) 41 42 7
Arithmetic expansion echo 1+1 echo $((1+1)) echo there are $((60*60*24)) seconds in a day x=10;y=20 echo $((x*2+y/4)) Arithmetic expansion cat > age_check >echo "how old are you?" >read age >echo "you'll be 60 in $((60-age)) years." 43 44 Order of expansion tofile= > log echo hello $tofile a=hello* echo $a echo $a 45 8