[devtac@test_1 shell]$ vi check_user.sh
#! /bin/sh
echo "You are logged in as `whoami`";
if [ `whoami` != devtac ]; then
echo "Must be logged in as devtac to run this script."
exit
fi
echo "Running script at `date`" 1.2运行结果
[devtac@test_1 shell]$ chmod a+x check_user.sh
[devtac@test_1 shell]$ ./check_user.sh
You are logged in as devtac
Running script at 2014年 12月 09日 星期二 13:35:17 CST 2.判断是否继续执行 2.1脚本
[devtac@test_1 shell]$ vi do_continue.sh
#! /bin/sh
doContinue=n
echo "Do you really want to continue? (y/n)"
read doContinue
if [ "$doContinue" != y ]; then
echo "Quitting..."
exit
fi
echo "OK... we will continue." 2.2运行结果
[devtac@test_1 shell]$ ./do_continue.sh
Do you really want to continue? (y/n)
y
OK... we will continue.
[devtac@test_1 shell]$ ./do_continue.sh
Do you really want to continue? (y/n)
n
Quitting...
[devtac@test_1 shell]$ 3 隐藏输入 3.1 脚本
[devtac@test_1 shell]$ vi hide_input.sh
#! /bin/sh
stty -echo
echo -n "Enter the database system password:"
read pw
stty echo
echo "$pw was entered" 3.2 结果
./hide_input.sh
Enter the database system password:123qweasd was entered
[devtac@test_1 shell]$ 3.3 解析 stty 命令 3.3.1 man 手册定义
DESCRIPTION
Print or change terminal characteristics.
[devtac@test_1 shell]$ vi is_a_directory.sh
#! /bin/sh
if [ -z "$1" ]; then
echo ""
echo "ERROR : Invalid number of arguments"
echo "Usage : $0 arg1"
echo ""
exit
fi
if [ -d $1 ]; then
echo "$1 is a directory."
else
echo "$1 is not a directory."
fi 4.2 测试结果
[devtac@test_1 shell]$ ./is_a_directory.sh
ERROR : Invalid number of arguments
Usage : ./is_a_directory.sh arg1
[devtac@test_1 shell]$ ./is_a_directory.sh $PWD
/home/devtac/shell is a directory. 4.3解析 4.3.1 脚本传参(未全部测试,仅作参考)
$0 脚本名字
$1 位置参数 #1
$2 - $9 位置参数 #2 - #9
${10} 位置参数 #10
$# 位置参数的个数
"$*" 所有的位置参数(作为单个字符串) *
"$@" 所有的位置参数(每个都作为独立的字符串)
${#*} 传递到脚本中的命令行参数的个数
${#@} 传递到脚本中的命令行参数的个数
$? 返回值
$$ 脚本的进程ID(PID)
$- 传递到脚本中的标志(使用set)
$_ 之前命令的最后一个参数
$! 运行在后台的最后一个作业的进程ID(PID) 4.3.2 判断参数是否为空,判读目录是否存在
-z string
True if the length of string is zero.
-d directory
True if the directory exists.
5.判读文件是否可读 5.1脚本
[devtac@test_1 shell]$ vi is_readable.sh
#! /bin/sh
if [ -z "$1" ]; then
echo ""
echo "ERROR : Invalid number of arguments"
echo "Usage : $0 agr1"
echo ""
exit
fi
if [ ! -r $1 ]; then
echo "$1 is NOT readable."
else
echo "$1 is readable."
fi
~ 5.2测试结果
[devtac@test_1 shell]$ ./is_readable.sh
ERROR : Invalid number of arguments
Usage : ./is_readable.sh agr1
[devtac@test_1 shell]$ ./is_readable.sh asdfas
asdfas is NOT readable.
[devtac@test_1 shell]$ ./is_readable.sh $PWD
/home/devtac/shell is readable.
[devtac@test_1 shell]$ ./is_readable.sh /home/devtac/
/home/devtac/ is readable.
[devtac@test_1 shell]$ ./is_readable.sh /home/devtac/shell/is_readable.sh
/home/devtac/shell/is_readable.sh is readable.
6 输出脚本参数 6.1 脚本
[devtac@test_1 shell]$ vi print_args.sh
#! /bin/sh
while [ $# -ne 0 ]
do
echo $1
shift
done 6.2 输出结果
[devtac@test_1 shelltemp2]$ vi copy_special_dir_file.sh
#! /bin/sh
#echo $#
# if the number of args is not equal 2 ,output command usage and exit
if [ $# -ne 2 ];then
echo "Usage : $0 fromdir todir"
exit 1
fi
fromdir=$1 #from directory
todir=$2 #to directory
#echo $fromdir $todir
#if fromdir or todir is not a valid directory ,exit
if [ ! -d $fromdir ] || [ ! -d $todir ];then
echo $fromdir or $todir is not a valid directory
exit 1
fi
for i in $fromdir/*; do
if [ -f $i ]; then
filename=${i#$fromdir/}
echo copying $i to $todir/$filename
cp -p $i $todir/$filename
fi
done
exit 0 7.2 测试结果
[devtac@test_1 shell]$ ./copy_special_dir_file.sh /home/devtac/shell /home/devtac/shelltemp2/qewqe
/home/devtac/shell or /home/devtac/shelltemp2/qewqe is not a valid directory
[devtac@test_1 shell]$ rm ../shelltemp2/*
[devtac@test_1 shell]$ ./copy_special_dir_file.sh /home/devtac/shell /home/devtac/shelltemp2
copying /home/devtac/shell/a.txt to /home/devtac/shelltemp2/a.txt
copying /home/devtac/shell/b.txt to /home/devtac/shelltemp2/b.txt
copying /home/devtac/shell/check_user.sh to /home/devtac/shelltemp2/check_user.sh
copying /home/devtac/shell/copy_special_dir_file.sh to /home/devtac/shelltemp2/copy_special_dir_file.sh
copying /home/devtac/shell/copy_special_dir_file.sh.bk to /home/devtac/shelltemp2/copy_special_dir_file.sh.bk
copying /home/devtac/shell/do_continue.sh to /home/devtac/shelltemp2/do_continue.sh
copying /home/devtac/shell/hide_input.sh to /home/devtac/shelltemp2/hide_input.sh
copying /home/devtac/shell/is_a_directory.sh to /home/devtac/shelltemp2/is_a_directory.sh
copying /home/devtac/shell/is_readable.sh to /home/devtac/shelltemp2/is_readable.sh
copying /home/devtac/shell/print_args.sh to /home/devtac/shelltemp2/print_args.sh
copying /home/devtac/shell/sh01.sh to /home/devtac/shelltemp2/sh01.sh
[devtac@test_1 shell]$ 7.3解析 7.3.1filename=${i#$fromdir/}
假设:i=/home/devtac/shell/a.txt
fromdir=/home/devtac/shell/a.txt
则:filename=a.txt
参考:来源http://linux.iyunv.com/techdoc/develop/2007/05/05/956956.shtml
为了完整起见,我这里再用一些例子加以说明 ${ } 的一些特异功能:
假设我们定义了一个变量为:
file=/dir1/dir2/dir3/my.file.txt
我们可以用 ${ } 分别替换获得不同的值:
${file#*/}:拿掉第一条 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最后一条 / 及其左边的字符串:my.file.txt
${file#*.}:拿掉第一个 . 及其左边的字符串:file.txt
${file##*.}:拿掉最后一个 . 及其左边的字符串:txt
${file%/*}:拿掉最后条 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:拿掉第一条 / 及其右边的字符串:(空值)
${file%.*}:拿掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
记忆的方法为:
# 是去掉左边(在鉴盘上 # 在 $ 之左边)
% 是去掉右边(在鉴盘上 % 在 $ 之右边)
单一符号是最小匹配﹔两个符号是最大匹配。
${file:0:5}:提取最左边的 5 个字节:/dir1
${file:5:5}:提取第 5 个字节右边的连续 5 个字节:/dir2
我们也可以对变量值里的字符串作替换:
${file/dir/path}:将第一个 dir 提换为 path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:将全部 dir 提换为 path:/path1/path2/path3/my