一、获取输入
1.命令行参数
#!/bin/bash
name=`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 add
3+4=7
2.特殊参数变量
1)$#
#!/bin/bash
param=$#
echo "total parameter is $#" 脚本运行时命令行参数的个数
echo "the last parameter is ${!#}" 不能在花括号内使用美元符,需要换成!号
2)$*与$@
#!/bin/bash
count=1
for param in "$*"
do
echo "\$* parameter-- $param"
done
for param in "$@"
do
echo "\$@ parameter-- $param"
done
# ./param one two three
$* parameter-- one two three $*将命令行中的所有参数当单个单词保存
$@ parameter-- one $@将命令行中的所有参数当同一字符串中的多个独立的单词
$@ parameter-- two
$@ parameter-- three
3.移动变量
#!/bin/bash
count=1
while [ -n "$1" ]
do
echo "current \$1 is $1"
count=$[ $count + 1 ]
shift 使用shift命令,默认情况每个参数变量减一,shift n 则移n位
done 参数移除后,无法恢复,$0不会改变
# ./shift one two three
current $1 is one
current $1 is two
current $1 is three
4.处理选项
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/bash
set -- `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
shift
done
count=1
for param in "$@"
do
echo "parameter #@count: $param"
count=$[ $count + 1 ]
done
2)getopts 标准格式:getopts optstring variable
#!/bin/bash
while 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";;
esac
done
shift $[ $OPTIND -1 ] OPTIND环境变量保存了参数列表中getopts正在处理的参数位置
count=1
for param in "$@"
do
echo "parameter $count: $param"
count=$[ $count + 1 ]
done
# ./getopts -a -b "one two" -s
found -a
found -b,with parameter one two 支持空格参数
unknown option: ? 未定义的选项统一以?号输出
5.获取用户输入
1.read
1)常规输入
#!/bin/bash
echo -n "enter your name:" -n,移除字符串末尾的换行符,使脚本用户紧随其后输入数据
read name 获得输入,指定变量名name,不指定则默认为变量REPLY
echo "welcome $name"
#!/bin/bash
read -p "enter your name:" name -p可直接在read命令行指定提示符
echo "welcome $name"
2)指定超时时间
#!/bin/bash
if read -t 3 -p "enter your name:" -t指定超时时间
then
echo "welcome $REPLY"
else
echo
echo "sorry time out"
fi
3)指定接收的字符数
#!/bin/bash
read -n1 -p "continue [Y/N]:" answer -n1,read命令在接受单个字符后退出
case $answer in
Y|y) echo
echo "ok continue";;
N|n) echo
echo "ok goodbye"
exit;;
esac
echo "end"
4)隐藏方式读取
#!/bin/bash
read -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/bash
echo "this is error line" >&2 临时重定向本行
echo "this is normal line"
# ./redirect 2> error 脚本中的错误行重定向到文件
this is normal line
# cat error
this is error line
2)永久性重定向输出
#!/bin/bash
exec 1> stdout.txt exec命令告诉shell在脚本执行期间重定向某个特定文件描述符
echo "this is error line" >&2
echo "this is normal line"
4.在脚本中重定向输入
#!/bin/bash
exec 0< stdin.txt
while read line read命令不再从标准输入(默认键盘)中读取,而从文件中读取
do
echo "$line"
done
5.创建新的输出文件描述符
#!/bin/bash
exec 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/bash
exec 6<&0 将STDIN文件描述符保存到文件描述符6
exec 0< stdin.txt
while read line
do
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,2
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 20371 root 0u CHR 136,3 5 /dev/pts/3
bash 20371 root 1u CHR 136,3 5 /dev/pts/3
bash 20371 root 2u CHR 136,3 5 /dev/pts/3
FD:文件描述符数目及访问类型(r读,w写,u读写)
NAME:文件名,标准输出即终端设备名
$$:特殊环境变量,进程的当前PID
9.阻止命令输出
# ls -al > /dev/null null文件不保存任何数据
# cat /dev/null > testfile 清空文件同时不删除文件的好办法
三、创建临时文件
1.创建临时本地文件
# mktemp tempfile.XXXXXX 文件名末尾加6个X
tempfile.V22488
2.创建/tmp目录下的临时文件
# mktemp -t tempfile.XXXXXX
/tmp/tempfile.A22529
3.创建临时目录
# mktemp -t -d tempdir.XXXXXX
/tmp/tempdir.v22556
四、记录消息
1.tee命令
# date | tee tee.txt 显示器显示消息的同时,记录到tee.txt文件
# date | tee -a tee.txt -a选项,追加到文件
1.命令行参数
#!/bin/bash
name=`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 add
3+4=7
2.特殊参数变量
1)$#
#!/bin/bash
param=$#
echo "total parameter is $#" 脚本运行时命令行参数的个数
echo "the last parameter is ${!#}" 不能在花括号内使用美元符,需要换成!号
2)$*与$@
#!/bin/bash
count=1
for param in "$*"
do
echo "\$* parameter-- $param"
done
for param in "$@"
do
echo "\$@ parameter-- $param"
done
# ./param one two three
$* parameter-- one two three $*将命令行中的所有参数当单个单词保存
$@ parameter-- one $@将命令行中的所有参数当同一字符串中的多个独立的单词
$@ parameter-- two
$@ parameter-- three
3.移动变量
#!/bin/bash
count=1
while [ -n "$1" ]
do
echo "current \$1 is $1"
count=$[ $count + 1 ]
shift 使用shift命令,默认情况每个参数变量减一,shift n 则移n位
done 参数移除后,无法恢复,$0不会改变
# ./shift one two three
current $1 is one
current $1 is two
current $1 is three
4.处理选项
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/bash
set -- `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
shift
done
count=1
for param in "$@"
do
echo "parameter #@count: $param"
count=$[ $count + 1 ]
done
2)getopts 标准格式:getopts optstring variable
#!/bin/bash
while 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";;
esac
done
shift $[ $OPTIND -1 ] OPTIND环境变量保存了参数列表中getopts正在处理的参数位置
count=1
for param in "$@"
do
echo "parameter $count: $param"
count=$[ $count + 1 ]
done
# ./getopts -a -b "one two" -s
found -a
found -b,with parameter one two 支持空格参数
unknown option: ? 未定义的选项统一以?号输出
5.获取用户输入
1.read
1)常规输入
#!/bin/bash
echo -n "enter your name:" -n,移除字符串末尾的换行符,使脚本用户紧随其后输入数据
read name 获得输入,指定变量名name,不指定则默认为变量REPLY
echo "welcome $name"
#!/bin/bash
read -p "enter your name:" name -p可直接在read命令行指定提示符
echo "welcome $name"
2)指定超时时间
#!/bin/bash
if read -t 3 -p "enter your name:" -t指定超时时间
then
echo "welcome $REPLY"
else
echo
echo "sorry time out"
fi
3)指定接收的字符数
#!/bin/bash
read -n1 -p "continue [Y/N]:" answer -n1,read命令在接受单个字符后退出
case $answer in
Y|y) echo
echo "ok continue";;
N|n) echo
echo "ok goodbye"
exit;;
esac
echo "end"
4)隐藏方式读取
#!/bin/bash
read -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/bash
echo "this is error line" >&2 临时重定向本行
echo "this is normal line"
# ./redirect 2> error 脚本中的错误行重定向到文件
this is normal line
# cat error
this is error line
2)永久性重定向输出
#!/bin/bash
exec 1> stdout.txt exec命令告诉shell在脚本执行期间重定向某个特定文件描述符
echo "this is error line" >&2
echo "this is normal line"
4.在脚本中重定向输入
#!/bin/bash
exec 0< stdin.txt
while read line read命令不再从标准输入(默认键盘)中读取,而从文件中读取
do
echo "$line"
done
5.创建新的输出文件描述符
#!/bin/bash
exec 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/bash
exec 6<&0 将STDIN文件描述符保存到文件描述符6
exec 0< stdin.txt
while read line
do
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,2
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 20371 root 0u CHR 136,3 5 /dev/pts/3
bash 20371 root 1u CHR 136,3 5 /dev/pts/3
bash 20371 root 2u CHR 136,3 5 /dev/pts/3
FD:文件描述符数目及访问类型(r读,w写,u读写)
NAME:文件名,标准输出即终端设备名
$$:特殊环境变量,进程的当前PID
9.阻止命令输出
# ls -al > /dev/null null文件不保存任何数据
# cat /dev/null > testfile 清空文件同时不删除文件的好办法
三、创建临时文件
1.创建临时本地文件
# mktemp tempfile.XXXXXX 文件名末尾加6个X
tempfile.V22488
2.创建/tmp目录下的临时文件
# mktemp -t tempfile.XXXXXX
/tmp/tempfile.A22529
3.创建临时目录
# mktemp -t -d tempdir.XXXXXX
/tmp/tempdir.v22556
四、记录消息
1.tee命令
# date | tee tee.txt 显示器显示消息的同时,记录到tee.txt文件
# date | tee -a tee.txt -a选项,追加到文件