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

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. 《Note --- Unreal 4 --- B project --- Second UV issue》

    Second uv 可以通过editor来生成: 这部分内容都是在staticMeshEditor这个文件夹下面的代码里: 关于UI的相应机制,有个文件UICommandList.cpp例如我点击st ...

  2. 用ajax查询天气

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <script src ...

  3. 时隔一年再读到the star

    The Star Arthur C. Clarke It is three thousand light-years to the Vatican. Once, I believed that spa ...

  4. CentOS 6.5 升级 PHP 到5.6

    1. 首先先看下已安装的版本: yum list installed | grep php 可以看到我的都是5.3的 php.x86_64 -.el6_8 @updates php-cli.x86_6 ...

  5. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  6. [LeetCode] Line Reflection 直线对称

    Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...

  7. visual studio for mac的安装初体验

    微软2016 Connect 大会发布了visuo studio for mac的pre版本,由于工作原因,现在工作环境是mac,虽然开发现在是在用python,但一直关注着.net的发展,于是自己很 ...

  8. LISA介绍及其使用方法

    LISA是ARM公司开发的一款开源工具.在内核开发过程中,苦于无法针对修改内容进行一些量化或者可视化结果的测量,而无感.LISA对于模型调优,回归测试都有较强的支持. 什么是LISA? LISA是Li ...

  9. 原生js实现fadein 和 fadeout

    js里面设置DOM节点透明度的函数属性:filter= "alpha(opacity=" + value+ ")"(兼容ie)和opacity=value/10 ...

  10. Android 第三方图表类 MPChart 的使用

    先看看条形图的的效果还不错是吧,实现这样的效果很合适呢! 还有折线图.饼图很多效果 效果不错对吧~ 下面我们就先来看看条形图的实现方法吧! 第一步: 引入第三方包 MPChart 如果你碰巧看过我之前 ...