linux shell -常用脚本

题记:来源与网络和自己工作中总结。有些脚本片段,函数经常使用。

 

1.判断登录用户

1.1脚本

[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]$ stty -a
speed 38400 baud; rows 47; columns 125; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

 

本例中使用的参数

       [-]echo
              echo input characters

屏蔽显示
stty -echo #禁止回显
stty echo #打开回显
测试方法:
stty -echo;read;stty echo;read

简述: 使用stty -echo 的效果 就像我们输入linux 登录密码时,看不到输入

 

 4 判断是否为目录

  4.1 脚本

[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 shell]$ ./print_args.sh 
[devtac@test_1 shell]$ ./print_args.sh asdf asdfasd asdfasdfas
asdf
asdfasd
asdfasdfas
[devtac@test_1 shell]$ 

6.3 解析

6.3.1 shift 命令

对于位置变量或命令行参数,其个数必须是确定的,或者当Shell程序不知道其个数时,可以把所有参数一起赋值给变量$*。若用户要求Shell在不知道位置变量个数的情况下,还能逐个的把参数一一处理,也就是在$1后为$2,在$2后面为$3等。在 shift命令执行前变量$1的值在shift命令执行后就不可用了。

7复制目录下文件到某个目录下

7.1脚本

[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.chinaunix.net/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

 

网络参考:

http://www.ha97.com/4020.html

http://blog.csdn.net/tianlesoftware/article/details/5381984

http://blog.csdn.net/qzwujiaying/article/details/6371246

http://linux.chinaunix.net/techdoc/develop/2007/05/05/956956.shtml

书籍:

http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html

posted @ 2014-12-09 13:59  tacg  阅读(1216)  评论(0编辑  收藏  举报