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

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日 星期二 :: 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 baud; rows ; columns ; line = ;
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 = ; time = ;
-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 $ ]; 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 $ ]; 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  ]
do
echo $
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

linux shell -常用脚本的更多相关文章

  1. Linux Shell常用脚本整理

    轮询检测Apache状态并启用钉钉报警◆ #!/bin/bash shell_user="root" shell_domain="apache" shell_l ...

  2. Linux Shell常用shell命令

    Linux Shell常用shell命令 一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示 ...

  3. [转帖]Linux Shell常用技巧(五)

    Linux Shell常用技巧(五) https://zhuanlan.zhihu.com/p/73451771 1. 变量:在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义.变量的类型可 ...

  4. [转帖]拿小本本记下的Linux Shell常用技巧(一)

    拿小本本记下的Linux Shell常用技巧(一) https://zhuanlan.zhihu.com/p/73361101 一. 特殊文件: /dev/null和/dev/tty Linux系统提 ...

  5. 【shell 大系】Linux Shell常用技巧

    在最近的日常工作中由于经常会和Linux服务器打交道,如Oracle性能优化.我们数据采集服务器的资源利用率监控,以及Debug服务器代码并解决其效率和稳定性等问题.因此这段时间总结的有关Linux ...

  6. Linux Shell常用技巧(目录)

    Linux Shell常用技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html一. 特殊文件: /dev ...

  7. shell常用脚本

    shell常用脚本 author:headsen chen  2017-10-17 15:36:17 个人原创,转载请注明,否则依法追究法律责任 1,vim  name.grep.sh 2,cat   ...

  8. Linux shell编写脚本部署pxe网络装机

    Linux shell编写脚本部署pxe网络装机 人工安装配置,Linux PXE无人值守网络装机  https://www.cnblogs.com/yuzly/p/10582254.html 脚本实 ...

  9. Linux Shell常用技巧(一) RE

    一.    特殊文件: /dev/null和/dev/tty Linux系统提供了两个对Shell编程非常有用的特殊文件,/dev/null和/dev/tty.其中/dev/null将会丢掉所有写入它 ...

随机推荐

  1. webdriver学习笔记

    该篇文章记录本人在学习及使用webdriver做自动化测试时遇到的各种问题及解决方式,问题比较杂乱.问题的解决方式来源五花八门,如有疑问请随时指正一遍改正. 1.WebDriver入门 //webdr ...

  2. 如果layer层在iframe下不居中滚动

    需要在layer前面加上parent.layer. 2.运用layer层的步骤: 1.引入1.8版本以上的jquery文件 <script type="text/javascript& ...

  3. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  4. 《使用Hibernate开发租房系统》内部测试笔试题

    笔试总结 1.在Hibernate中,以下关于主键生成器说法错误的是( C). A.increment可以用于类型为long.short或byte的主键 B.identity用于如SQL Server ...

  5. 索引超出了数组界限(Microsoft.SqlServer.Smo)

    SSMS连接远程SQL Server服务器是很方便的. 昨天我用SQL Server 2008 SSMS连接SQL Server 2012竟然报错,如下图: 在网上搜了,参考这个参考那个,太啰嗦了,确 ...

  6. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  7. Mysql查询——深入学习

    1.开篇 之前上一篇的随笔基本上是单表的查询,也是mysql查询的一个基本.接下来我们要看看两个表以上的查询如何得到我们想要的结果. 在学习的过程中我们一起进步,成长.有什么写的不对的还望可以指出. ...

  8. 较为完整的meta

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. [板子]ISAP

    ISAP求最大流,敲了一发板子,无压行,教程略去.转载请随意. #include <cstdio> #include <cstring> #include <algori ...

  10. ffmpeg 音频转换(amr2mp3)

    yasm:http://yasm.tortall.net/Download.html(汇编器,新版本的ffmpeg增加了汇编代码) lame:http://lame.sourceforge.net/d ...