linux shell -常用脚本
题记:来源与网络和自己工作中总结。有些脚本片段,函数经常使用。
1.判断登录用户
1.1脚本
[devtac@test_1 shell]$ vi check_user.sh#! /bin/shecho "You are logged in as `whoami`";if [ `whoami` != devtac ]; thenecho "Must be logged in as devtac to run this script."exit fiecho "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/shdoContinue=n echo "Do you really want to continue? (y/n)" read doContinueif [ "$doContinue" != y ]; thenecho "Quitting..."exit fiecho "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/shstty -echo echo -n "Enter the database system password:" read pw stty echoecho "$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 手册定义
DESCRIPTIONPrint 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
本例中使用的参数
[-]echoecho 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/shif [ -z "$1" ]; thenecho ""echo "ERROR : Invalid number of arguments"echo "Usage : $0 arg1"echo ""exit fiif [ -d $1 ]; thenecho "$1 is a directory." elseecho "$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/shif [ -z "$1" ]; thenecho ""echo "ERROR : Invalid number of arguments"echo "Usage : $0 agr1"echo ""exit fiif [ ! -r $1 ]; thenecho "$1 is NOT readable." elseecho "$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/shwhile [ $# -ne 0 ]doecho $1shiftdone
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
转载于:https://www.cnblogs.com/tacg/p/4153068.html
linux shell -常用脚本相关推荐
- linux Shell(脚本)编程入门实例讲解详解
linux Shell(脚本)编程入门实例讲解详解 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集, ...
- Linux Shell常用技巧(一)
Linux Shell常用技巧 文章转载自:http://www.cnblogs.com/stephen-liu74/archive/2011/12/20/2285454.html 一. 特殊文 ...
- Linux Shell常用技巧(三) sed
Linux Shell常用技巧(三) sed 八.流编辑器sed 8.1 sed简介 sed是stream editor的缩写,一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区 ...
- Linux shell 自启动脚本写法
# ********************************************************************** # Linux shell 自启动脚本写法 # 说明: ...
- Linux Shell常用技巧(目录)
Linux Shell常用技巧(一) http://liuxuyang.blog.51cto.com/2546552/1289413 一. 特殊文件: /dev/null和/dev/tty 二. 简单 ...
- 【shell 大系】Linux Shell常用技巧
在最近的日常工作中由于经常会和Linux服务器打交道,如Oracle性能优化.我们数据采集服务器的资源利用率监控,以及Debug服务器代码并解决其效率和稳定性等问题.因此这段时间总结的有关Linux ...
- Linux Shell常用技巧(九) 系统运行进程
Linux Shell常用技巧(九) 系统运行进程 十九. 和系统运行进程相关的Shell命令: 1. 进程监控命令(ps): 要对进程进行监测和控制,首先必须要了解当 ...
- android adb 分析,android adb shell常用脚本分析课件.ppt
android adb shell常用脚本分析课件 ADB概要 Android 调试系统是一个面对客户服务系统,包括三个组成部分: 电脑上运行的客户端. 在你用于开发的机器上作为后台进程运行的服务器. ...
- Linux shell 常用代码片断
Linux shell 常用代码片断 参数输入 echo "input tips:" read input_variable echo "You entered: $in ...
最新文章
- Python 使用 Flask框架记录
- 世界上最百变的人不是女友,竟然是......
- 【消息中间件】Spring整合RabbitMQ
- chmod 是一条在Unix系统中用于控制用户对文件的权限的命令
- ASP.NET 构建高性能网站 架构设计
- linux redis客户端,Redisson 3.4.0和2.9.0发布,Redis客户端
- C++基础语言知识大汇总(不断更新!!!)
- nginx php-fpm安装配置 CentOS编译安装php7.2
- TCP/IP四层协议模型与ISO七层模型(TCP/IP系统学习(2))
- 扩展欧几里得算法求逆元c语言,利用扩展欧几里得算法编程求逆元
- Linux 硬盘读写测速
- 信息系统项目管理师考试大纲(第2版)
- GD32库中常用的位操作REGIDX_BIT(regidx, bitpos)
- 《星际争霸2》人工智能研究环境 SC2LE 初体验
- 虚幻引擎(UE4) UMG 创建菜单
- linux统计单拷贝基因家族,为什么要进行基因家族分析?
- 微信订阅号推广只要这一个标题就够了!
- 爬虫入门 ---- CSDN查看文章全部评论
- diamond 学习
- 高通骁龙X55 5G modem信息图和骁龙X55调制解调器简介
热门文章
- 文档服务器备份策略,云服务器备份备份策略管理
- [转] 最火的42部美剧,练听力的不二之选
- mysql增加数据 条件,mysql根据条件决定是否插入数据
- 数据挖掘总结之消极学习与积极学习
- 读取远程服务器上文件内容,读取远程服务器上的文件
- Unity Animation Instancing 官方解决方案初试
- 华为注册“花瓣编程”,释放了什么信号?
- sau交流学习社区第三方登陆github--oauth来实现用户登录
- 谷歌与DeepMind的控制权之战,刚刚开始
- GeoStru.CVSoil.v2014.7.2.47 1CD