我的新浪微博:http://weibo.com/u/1602714773
CSDN博客:http://blog.csdn.net/hunterno4
Linux Shell笔记之输入与输出
上一篇 /
下一篇 2013-11-10 16:56:37
/ 个人分类:Linux
一、获取输入1.命令行参数#!/bin/bashname=`basename $0` $0参数获取shell在命令行启动的程序的名字,basename命令去掉路径echo "current command is $name"if [ -n "$1" ] && [ -n "$2" ] $1、$2分别代表第一个参数与第二个参数,第9个变量后,需加花括号then 如${10},-n判断参数是否非空 sum=$[ $1 + $2 ] echo "$1+$2=$sum"else echo "please input two number"fi[root@localhost hunterno4]# ./add 3 4 参数若含空格需加引号current command is add3+4=72.特殊参数变量1)$##!/bin/bashparam=$#echo "total parameter is $#" 脚本运行时命令行参数的个数echo "the last parameter is ${!#}" 不能在花括号内使用美元符,需要换成!号2)$*与$@#!/bin/bashcount=1for param in "$*"do echo "\$* parameter-- $param"donefor param in "$@"do echo "\$@ parameter-- $param"done# ./param one two three$* parameter-- one two three $*将命令行中的所有参数当单个单词保存$@ parameter-- one $@将命令行中的所有参数当同一字符串中的多个独立的单词$@ parameter-- two$@ parameter-- three3.移动变量#!/bin/bashcount=1while [ -n "$1" ]do echo "current \$1 is $1" count=$[ $count + 1 ] shift 使用shift命令,默认情况每个参数变量减一,shift n 则移n位done 参数移除后,无法恢复,$0不会改变# ./shift one two threecurrent $1 is onecurrent $1 is twocurrent $1 is three4.处理选项1)getopt 格式化命令行选项与参数:getopt optstring options parameters# getopt -q ab:cd -a -b test1 -cd test2 test3 ab:cd定义了命令行有效的选项字母,在需要参数值的字母后加:号 -a -b test1 -c -d -- test2 test3 -q忽略错误信息,getopt命令不能处理含空格参数#!/bin/bashset -- `getopt -q ab:c "$@"`while [ -n "$1" ]do case "$1" in -a) echo "found -a";; -b) param="$2" echo "found -b,with parameter $param" shift;; -c) echo "found -c";; --) shift 参数和选项默认用--特殊字符分隔,碰到--则结束选项处理,进入参数处理流程 break;; *) echo "$1 is not a option" esac shiftdonecount=1for param in "$@"do echo "parameter #@count: $param" count=$[ $count + 1 ]done2)getopts 标准格式:getopts optstring variable#!/bin/bashwhile getopts :ab:cd opt optstring前加:号忽略错误消息,getopts将当前参数保存在variable中do case "$opt" in a) echo "found -a";; case定义中不需再用单破折线 b) echo "found -b,with parameter $OPTARG";; OPTARG环境变量保存选项需要的参数值 c) echo "found -c";; d) echo "found -d";; *) echo "unknown option: $opt";; esacdoneshift $[ $OPTIND -1 ] OPTIND环境变量保存了参数列表中getopts正在处理的参数位置count=1for param in "$@"do echo "parameter $count: $param" count=$[ $count + 1 ]done# ./getopts -a -b "one two" -sfound -afound -b,with parameter one two 支持空格参数unknown option: ? 未定义的选项统一以?号输出5.获取用户输入1.read1)常规输入#!/bin/bashecho -n "enter your name:" -n,移除字符串末尾的换行符,使脚本用户紧随其后输入数据read name 获得输入,指定变量名name,不指定则默认为变量REPLYecho "welcome $name"#!/bin/bashread -p "enter your name:" name -p可直接在read命令行指定提示符echo "welcome $name"2)指定超时时间#!/bin/bashif read -t 3 -p "enter your name:" -t指定超时时间then echo "welcome $REPLY"else echo echo "sorry time out"fi3)指定接收的字符数#!/bin/bashread -n1 -p "continue [Y/N]:" answer -n1,read命令在接受单个字符后退出case $answer in Y|y) echo echo "ok continue";; N|n) echo echo "ok goodbye" exit;;esacecho "end"4)隐藏方式读取#!/bin/bashread -s -p "enter your passwd:" passwd -s,阻止传给read命令的数据显示在显示器上(背景色一样)echo echo "passwd right? $passwd"二、重定向输入与输出1.文件描述符Linux中将每个对象当作文件处理,包括输入与输出,用文件描述符来标识每个对象,共9个文件描述符0 STDIN 标准输入1 STDOUT 标准输出2 STDERR 标准错误2.重定向输入与输出# cat < testfile 重定向cat的输入# ls -l > test2file 重定向ls -l命令的输出到test2file文件,>>则这追加到文件末尾# ls -al badfile 2> test4 重定向错误# ls -al read badfile 2> test5 1> test6 同时重定向标准错误与标准输出 # ls -al read badfile &> test7 同时重定向标准错误与标准输出,错误消息会显示在最前3.在脚本中重定向输出1)临时重定向输出#!/bin/bashecho "this is error line" >&2 临时重定向本行echo "this is normal line"# ./redirect 2> error 脚本中的错误行重定向到文件this is normal line# cat error this is error line2)永久性重定向输出#!/bin/bashexec 1> stdout.txt exec命令告诉shell在脚本执行期间重定向某个特定文件描述符echo "this is error line" >&2echo "this is normal line"4.在脚本中重定向输入#!/bin/bashexec 0< stdin.txtwhile read line read命令不再从标准输入(默认键盘)中读取,而从文件中读取do echo "$line"done5.创建新的输出文件描述符#!/bin/bashexec 3>&1 文件描述符3重定向到文件描述符1,即显示器exec 1> testfile 文件描述符1重定向至testfile文件echo "this is a line should be in testfile"exec 1>&3 文件描述符1重定向到文件描述符3,即回到显示器echo "back to normal"6.创建新的输入文件描述符#!/bin/bashexec 6<&0 将STDIN文件描述符保存到文件描述符6exec 0< stdin.txtwhile read linedo echo "$line"done exec 0<&6 恢复文件描述符0为从显示器中读取数据read -p "enter your name:"echo "your name $REPLY"7.关闭文件描述符exec 3>&- 脚本结束前手动关闭文件描述符8.列出打开的文件描述符# lsof -a -p $$ -d 0,1,2COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEbash 20371 root 0u CHR 136,3 5 /dev/pts/3bash 20371 root 1u CHR 136,3 5 /dev/pts/3bash 20371 root 2u CHR 136,3 5 /dev/pts/3FD:文件描述符数目及访问类型(r读,w写,u读写)NAME:文件名,标准输出即终端设备名$$:特殊环境变量,进程的当前PID9.阻止命令输出# ls -al > /dev/null null文件不保存任何数据# cat /dev/null > testfile 清空文件同时不删除文件的好办法三、创建临时文件1.创建临时本地文件# mktemp tempfile.XXXXXX 文件名末尾加6个Xtempfile.V224882.创建/tmp目录下的临时文件# mktemp -t tempfile.XXXXXX/tmp/tempfile.A225293.创建临时目录# mktemp -t -d tempdir.XXXXXX/tmp/tempdir.v22556四、记录消息1.tee命令# date | tee tee.txt 显示器显示消息的同时,记录到tee.txt文件# date | tee -a tee.txt -a选项,追加到文件
收藏
举报
TAG:
Linux
linux
shell
Shell
重定向
输入输出
参数获取