$ x=supercalifragilisticexpialidocious
$ echo There are ${#x} characters in $x
#-----------------------------------------------------------
µ²ªG:
There are 34 characters in supercalifragilisticexpialidocious
%getname "Yao Nan" Lien
You have entered: Yao Nan Lien
=== Passing $* ===
First Name: Yao
Last Name: Nan
=== Passing $@ ===
First Name: Yao
Last Name: Nan
=== Passing "$*" ===
First Name: Yao Nan Lien
Last Name:
=== Passing "$@" ===
First Name: Yao Nan
Last Name: Lien
# -------------------------------------
# Script: singlequote
# -------------------------------------
name=Clinton
echo 'My name is '$name' and today is '`date`
# -------------------------------------
# Script: doublequote
# -------------------------------------
name=Clinton
echo "My name is $name and today is `date`"
# -------------------------------------
# Script: doublequote2
# -------------------------------------
name=Clinton
output="My name is $name and today is `date`"
while IFS=: read user password uid realname homedir shell
do
echo user $user is $realname who prefers $shell
done < /etc/passwd
cat /etc/passwd |
while IFS=: read user password uid realname homedir shell
do
echo user $user is $realname who prefers $shell
done
Wed Apr 16 07:34:13 CST 2025
Untitled Document
Examples of Using Read
½Æ»s¾ãӥؿý
1 §Q¥Î copy -r directory «ü¥O
2. §Q¥Î¤U±¨âÓ script
# -----------------------------------------------
# Script cpdir v1
# -----------------------------------------------
find /a/b -type d -print | #Find all directories
sed 's;/a/b/;/home/b/;' | #Change name
sed 's/^/mkdir /' | #Insert mkdir command
sh -x #Execute
# -----------------------------------------------
# Script cpdir v2
# -----------------------------------------------
find /a/b -type d -print | #Find all directories
sed 's;/a/b/;/home/b/;' | #Change name
while read newdir #Read new directory name
do
mkdir $newdir #Make new directory
done
Wed Apr 16 07:34:13 CST 2025
Untitled Document
¬yµ{±±¨î
Wed Apr 16 07:34:14 CST 2025
Untitled Document
Target of Condition Test
Wed Apr 16 07:34:14 CST 2025
Untitled Document
POSIX Defined Exit Status
Wed Apr 16 07:34:14 CST 2025
Untitled Document
Test
Wed Apr 16 07:34:14 CST 2025
Untitled Document
¬yµ{±±¨î - if
Wed Apr 16 07:34:14 CST 2025
Untitled Document
¬yµ{±±¨î - If ¤§¨Ò
Wed Apr 16 07:34:14 CST 2025
Untitled Document
¬yµ{±±¨î - If ¤§¨Ò
Wed Apr 16 07:34:15 CST 2025
Untitled Document
Nested if Structure
if
write stu1 < message
then
:
else
if mail stu1 < message
then
echo "mail sent instead"
else
echo "Could not sent instead"
fi
fi
Wed Apr 16 07:34:15 CST 2025
Untitled Document
¬yµ{±±¨î - for °j°é
¬yµ{±±¨î - for °j°é
Wed Apr 16 07:34:17 CST 2025
Untitled Document
¬yµ{±±¨î - for
¬yµ{±±¨î - for
Wed Apr 16 07:34:17 CST 2025
Untitled Document
More Codes
for person in stu1 stu2 stu3
do
if
who | grep $person > /dev/null
then
write $person < message
else
mail $person < message
fi
done
q=`who -T | grep "^s8500" | cut -c10`
if test "$q" = "+"
then
write s8500 < note
else
mail s8500 < note
fi
Wed Apr 16 07:34:17 CST 2025
Untitled Document
Numerical Comparison
test "$n1" -eq "$n2"
Equal
test "$n1" -ne "$n2"
Not equal
test "$n1" -gt "$n2"
Greater than
test "$n1" -ge "$n2"
Greater than or equal to
test "$n1" -lt "$n2"
Less than
test "$n1" -le "$n2"
Less than or equal to
¨Ò
# -----------------------------------------------
# countuser: count number of users on the system
# -----------------------------------------------
count=`who | wc -i`
if [ "$count" -le "20" ]
then
echo "$count" users - light load
else
echo "$count" users - heavy load
fi
Wed Apr 16 07:34:17 CST 2025
Untitled Document
¬yµ{±±¨î - for °j°é
¬yµ{±±¨î - for °j°é
Wed Apr 16 07:34:17 CST 2025
Untitled Document
¬yµ{±±¨î - for
¬yµ{±±¨î - for
Wed Apr 16 07:34:17 CST 2025
Untitled Document
While and Until
Syntax
while
command_lines
do
command_lines
done
until
command_lines
do
command_lines
done
'while' will test the condition to do the first iteration
'until' will do the first iteration before test the condition
Begin
while marks loop start
Control
Evaluate return code from last command before do
Middle
execute all commands within body
End
done marks loop end, shell reads until end is found
¨Ò Script: Appends stdin to file outfile
echo "
Enter lines of input (end with ^D):
"
while read x
do
echo $x
done >> outfile
file=
verbose=
quiet=
long=
while [ $# -gt 0 ] #Loop until no args left
do
case $1 in #Check first arg
-f) file=$2
shift #Shift off "-f" so that shift at end gets value in $2
;;
-v) verbose=true; quiet= ;;
-q) quiet=true; verbose= ;;
-l) long=true ;;
--) shift #By convention, - - ends options
break
;;
-*) echo $0: $1: unrecognized option >&2 ;;
*) break ;; # Nonoption argument, break while loop
esac
shift #Set up for next iteration
done
# -----------------------------------------------
# Send emails to top 10 disk space usage users
# -----------------------------------------------
cd /home #Move to top of home directories
du -s * | #Generate raw disk usage
sort -nr | #Sort numerically, highest numbers first
sed 10q | #Stop after first 10 lines
while read amount name
do
mail -s "disk usage warning" $name << EOF
Greetings. You are one of the top 10 consumers of disk space
on the system. Your home directory uses $amount disk blocks.
Please clean up unneeded files, as soon as possible.
Thanks,
Your friendly neighborhood system administrator.
EOF
done
Wed Apr 16 07:34:22 CST 2025
Untitled Document
Open a file for both input and output
program <> file
To open file for both reading and writing.
It is up to program to be aware of this and take advantage of it
not practically useful
An undocumented feature in V7 Bourne shell
Standardized in the 1992 POSIX standard,
on many systems /bin/sh doesn't support it.
Wed Apr 16 07:34:22 CST 2025
Untitled Document
File Descriptor Manipulation
§ó½ÆÂøªºI/OÂà¦V
#------ ------------------------------------------------
# sends standard output (file descriptor 1) to 'outfile'
# and standard error (file descriptor 2) to 'errfile'.
# -----------------------------------------------------
myscript 1> ourfile 2> errfile
# ---------------------------------------
# sends standard output to 'outfile'
# and throw away standard error.
# ---------------------------------------
myscript 1> ourfile 2> /dev/null
# ----------------------------------------------------------
# sends both standard output and standard error to 'outfile'
# ----------------------------------------------------------
myscript > ourfile 2>&1
# -------------------------------------------------------
# sends both standard output and standard error to pipe
# -------------------------------------------------------
myscript 2>&1 | nextscript
A pseudosignal used by the shell, indicating
that the standard output
has hung up; sending this signal logs you out.
2
INT
Sent by a <Del> keystroke; sends an interrupt to
the current program.
3
QUIT
Sent by a <Ctrl>\ keystroke; causes the current
program to abort,
leaving behind a core dump (for use in program debugging).
9
KILL
Cannot be trapped or ignored; forces the
receiving program to die.
11
SEGV
Indicates that a segmentation violation (a
memory fault within the
UNIX system) has occurred. This signal cannot be trapped or
ignored by a
shell; it invariably terminates the receiving process and causes
a core dump.
15
TERM
Terminates the receiving program. (This signal
should be used in
preference to Signal 9, because the receiving program can catch
it and carry
out some shutdown operation, for example closing open files;
Signal 9 forces
the process to die immediately.)