Professional Documents
Culture Documents
Built-in commands
Scripts
Redirection
Wildcards
CORE Shell Functionality
Pipes
Subshells
Background processing
Command substitution
Invoking the Shell
$ ls
$ ls | sort | \
lp
Built-in commands
echo arguments
$ echo Hi, How are you?
Hi, How are you?
cd dir
Metacharacters
Output redirection
Input redirection
File-substitution wildcards:
* matches 0 or more characters
? matches any single character
[...] matches any character within brackets
Command substitution:
`command` replaced by the output of command
e.g. echo `ls`
Metacharacters
| Pipe
Conditional execution
|| execute command if previous one fails
&& execute command if previous one succeeds
Metacharacters
# Comment
rest of characters ignored by shell
Examples:
cat x1.c > y.c
cat x2.c >> y.c
mail tony < hiMom
$ ls
ppp00* ppp24* ppp48* ppp72*
$ ls | wc -w
4
Pipes
$ head -4 /etc/passwd
root:fjQyH/FG3TJcg:0:0:root:/root:/bin/sh
bin:*:1:1:bin:/bin:
daemon:*:2:2:daemon:/sbin:
adm:*:3:4:adm:/var/adm:
$ cat /etc/passwd | awk -F: '{print $1}' | sort
adm
bin
daemon
raj
tee utility
$ more who.capture
raj tty1 Jun 19 09:31
naveen ttyp0 Jun 19 20:17 (localhost)
Command Substitution
Example:
$date; pwd; ls
$date > date.txt; pwd > pwd.txt; ls
Sequences
$ date; pwd; ls
Sat Jun 19 22:33:19 EDT 2007
/home/raj/oracle
jdbc/ ows/ proc/ sql/ sqlj/ who.capture
$ date > date.txt; pwd > pwd.txt; ls
date.txt jdbc/ ows/ proc/ pwd.txt sql/ sqlj/
who.capture
Sequences
Conditional sequences:
Example:
$ more out.txt
Grouping commands
$ (date; ls; pwd) > out.txt
$ more out.txt
Sat Jun 19 22:40:43 EDT 2007
date.txt
jdbc/
out.txt
ows/
proc/
pwd.txt
sql/
sqlj/
who.capture
/home/raj/oracle
Background processing
Starts a sub-shell
commands are executed as a background process
does not take control of the keyboard
#!/bin/csh
# A simple C-shell script
echo -n "The date today is "
date
Subshells
Depends on shell:
variable="value"
export variable
Assigning values to variables
set variable="value"
$$ process ID of shell
$echo3*4=12
33.log3.texscript.cshscript2.csh4=12
$echo'3*4=12'
3*4=12
$echo"mynameis$USER;thedateis`date`"
mynameisraj;thedateisSunJun2021:59:13EDT2007
Here Documents
$cathere.csh
mail$1<<ENDOFTEXT
Dear$1,
Pleaseseemeregardingsomeexcitingnews!
$USER
ENDOFTEXT
echomailsentto$1
$here.cshraj
mailsenttoraj
Here Example
$mail
Mailversion8.16/6/93.Type?forhelp.
"/var/spool/mail/raj":6messages1new
5raj@kamakshi.gsu.eduSunJun2022:1318/420
>N6raj@kamakshi.gsu.eduSunJun2022:1414/377
&
Message6:
FromrajSunJun2022:14:312007
Date:Sun,20Jun200722:14:310400
From:raj@kamakshi.gsu.edu
To:raj@kamakshi.gsu.edu
Dearraj,
Pleaseseemeregardingsomeexcitingnews!
raj
Job Control
$ ps -efl
PID : process ID
nohup Command
Homebrewed utilities:
Some Unix users create their own utilities
Stored in their bin directory
Place their bin directory ahead of all others
Their version of the utility is executed
Termination and Exit codes
eval command
The eval shell command executes the output of the
command as a regular shell command.
$eval`echox=5`
$echo$x
5
Common Core Built-in commands
exec command
The exec shell command causes the shell's image
to be replaced with the command in the process'
memory space.
As a result, if the command terminates, the shell
also ceases to exist; If the shell was a login shell,
the login session terminates.
Common Core Built-in commands
shift
This command causes all of the positional
parameters $2..$n to be renamed $1..$(n-1) and $1
is lost.
Useful in processing command line parameters.
Common Core Built-in commands
$catscript3.csh
#!/bin/csh
echofirstargumentis$1,allargsare$*
shift
echofirstargumentis$1,allargsare$*
$script3.cshabcd
firstargumentisa,allargsareabcd
firstargumentisb,allargsarebcd
umask Command