当前位置 博文首页 > Hello, New World!:《linux命令行与shell脚本编程大全》第三版
/* 位置参数演示 */
#!/bin/bash
# using one command line parameter
#
factorial=1
for (( number = 1; number <= $1 ; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo "The factorial of $1 is $factorial."
[~/shell/4]$: test1.sh 5
/* 位置参数变量传递字符串 */
#!/bin/bash
# testing string parameters
#
echo Hello $1, glad to meet you.
[~/shell/4]$: test3.sh "Rich Blum"
/* basename 和 $0 演示*/
#!/bin/bash
# Using basename with the $0 parameter
#
name=$(basename $0)
echo The script name is: $name
[~/shell/4]$: bash /home/jiangyuan/shell/4/test2.sh
/* 不同功能的脚本 */
#!/bin/bash
# Testing a Multi-function script
#
name=$(basename $0)
#
if [ $name = "addem" ]
then
total=$[ $1 + $2 ]
#
elif [ $name = "multem" ]
then
total=$[ $1 * $2 ]
fi
#
echo
echo The calculated value is $total
[~/shell/4]$: cp test6.sh addem
if [ -n "$1" ]
then
echo Hello $1, glad to meet you.
else
echo "Sorry, you did not identify yourself. "
fi
/* $* 和 $@ */
#!/bin/bash
# testing $* and $@
#
count=1
#
for param in "$*"
do
echo "\$* Parameter #$count = $param"
count=$[ $count + 1 ]
done
#
count=1
#
for param in "$@"
do
echo "\$@ Parameter #$count = $param"
count=$[ $count + 1 ]
done
[~/shell/4]$: test3.sh 1 2 3 4
/* shift命令演示 */
#!/bin/bash
# test shift command
count=1
while [ -n "$1" ]
do
echo "Param #$count=$1"
count=$[ $count + 1 ]
shift
done
[~/shell/4]$: test4.sh hello world how are you
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done
/* --双破折线演示 */
#!/bin/bash
# extracting options and parameters
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) echo "found the -b option";;
-c) echo "found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in $@
do
echo "param #$count: $param"
count=$[ $count + 1 ]
done
[~/shell/4]$: test5.sh -a -a -b test1.sh test2.sh test3.sh
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
$: ./test17.sh -a -b test1 -d
/* getopt 脚本演示 */
#!/bin/bash
# extracting options and parameters
set -- $(getopt -q ab:cd "$@")
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) param="$2"
echo "found the -b option, with param value $param"
shift;;
-c) echo "found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in $@
do
echo "param #$count: $param"
count=$[ $count + 1 ]
done
[~/shell/4]$: test6.sh -a -b test1 -cd test2 test3 test4
/* getopts 脚本演示 */
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt";;
esac
done
$: ./test19.sh -b "test1 test2" -a
/* read 演示 */
echo -n "Enter your name: " # -n 可以去除echo输出行的最后一个\n符号
read name
echo "Hello $name, welcome to my program. "
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days days old! "
[~/shell/4]$: test7.sh
/* read 统计输入字符数演示 */
#!/bin/bash
# getting just one character of input
#
read -n2 -p "Do you want to continue [Y/n]? " answer
case $answer in
Y | y) echo
echo "fine, continue on…";;
N | n) echo
echo "OK, goodbye…"
exit;;
*) echo
echo "fine, continue on…";;
esac
echo "This is the end of the script."
[~/shell/4]$: test8.sh
/* read 读取文件内容 */
#!/bin/bash
# test read file
count=1
cat $1 | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the $1."
[~/shell/4]$: test10.sh test10.sh
/* 临时重定向演示 */
$: cat test8
#!/bin/bash
# testing STDERR messages
echo "This is an error" >&2
echo "This is normal output"
$: ./test8
/* exec永久重定向演示 */
#!/bin/bash
# redirecting all output to a file
exec 1>$1
echo "This is a test of redirecting all output"
echo "from a script to another file."
echo "without having to redirect every individual line"
$ ./test10 testfile
/* 演示 */
#!/bin/bash
# using an alternative file descriptor
exec 3>test13out
# exec 3>>test13out2 追加到文件中去
echo "This should display on the monitor"
echo "and this should be stored in the file" >&3
echo "Then this should be back on the monitor"
$: ./test13
/* 演示 */
#!/bin/bash
# redirecting input file descriptors
touch testfile
exec 6<&0
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
exec 0<&6
read -n2 -p "Are you done now? [Y/n]" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "Sorry, this is the end.";;
*) echo "Goodbye";;
esac
// 文件描述符6用来保存STDIN的位置。然后脚本将STDIN重定向到一个文件。read命令的所有输入都来自重定向后的STDIN(也就是输入文件)。在读取了所有行之后,脚本会将STDIN重定向到文件描述符6,从而将STDIN恢复到原先的位置。该脚本用了另外一个read命令来测试STDIN是否恢复正常了。这次它会等待键盘的输入。
/* 演示 */
#!/bin/bash
# testing closing file descriptors
exec 3> test17file
echo "This is a test line of data" >&3
exec 3>&-
echo "This won't work" >&3
$: test17.sh
/* 演示多个替代性文件描述符 */
#!/bin/bash
# testing lsof with file descriptors
exec 3> test18file1
exec 6> test18file2
exec 7< testfile
/usr/bin/lsof -a -p $$ -d0,1,2,3,6,7
$: test18.sh
/* 演示 */
#!/bin/bash
# creating and using a temp file
tempfile=$(mktemp test19.XXXXXX)
exec 3>$tempfile
echo "This script writes to temp file $tempfile"
echo "This is the first line" >&3
echo "This is the second line." >&3
echo "This is the last line." >&3
exec 3>&-
echo "Done creating temp file. The contents are:"
cat $tempfile
rm -f $tempfile 2> /dev/null
$: test19.sh
/* 重定向实例(操作.csv文件到SQL数据库) */
#!/bin/bash
# read file and create INSERT statements for MySQL
outfile='members.sql'
IFS=','
while read lname fname address city state zip
do
cat >> $outfile << EOF
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('$lname', '$fname', '$address', '$city', '$state', '$zip');
EOF
done < ${1}
$: cat members.csv
/* trap命令演示 */
#!/bin/bash
# Testing signal trapping
trap "echo 'Sorry! I have trapped Ctrl+C'" SIGINT
echo This is a test script
count=1
while [ $count -le 10 ]
do
echo "Loop #$count"
sleep 1
count=$[ $count + 1 ]
done
$: test1.sh
/* 演示 */
#!/bin/bash
# Trapping the script exit
#
trap "echo Goodbye..." EXIT
#
count=1
while [ $count -le 5 ]
do
echo "Loop #$count"
sleep 1
count=$[ $count + 1 ]
done
$: test2.sh