目录
54. 位置参数 $0 $9 ${10}
$0 代表脚本名
$1 ~ $9 代表 第1~9个参数名,
${10}, ${11}, ${12} 代表 第10,11,12...个参数名。
#!/bin/bash
name=$(basename $0)
arg1=$1 arg2=$2 arg3=$3 arg4=$4 arg5=$5
arg6=$6 arg7=$7 arg8=$8 arg9=$9
arg10=${10} arg11=${11}
echo "The script name is $name"
echo "The arguments are $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8 $arg9 $arg10 $arg11"
55. 特殊参数 $# $* $@
$# 代表命令行 参数的个数
${!#} 代表命令行最后一个参数, 因为${$#}这种写法是错误的!
#!/bin/bash
if [ -n "${12}" ]; then # Check the 12th arg exists or not
echo "The 12th arg is " ${12}
else
echo "The 12th arg does not exist"
fi
echo "The are totally " $# " arguments."
echo "The last argument is" ${!#}
$* 变量 会将命令行上提供的所有参数当作一个单词保存
$@ 变量 会将命令行上提供的所有参数当作同一字符串中的多个独立的单词
#!/bin/bash
for param in "$*"
do
echo $param
done
echo "\$* finished, now \$@"
for param1 in "$@"
do
echo $param1
done
56. 移动变量 shift
shift 将每个参数变量向左移动1个位置。
shift n 将每个参数变量向左移动n个位置。
#!/bin/bash
echo "The original parameters: $*"
shift 2
echo "Here's the new parameter: $*"
57. read命令 -p -t -n -s
read命令 从标准输入(键盘)或另一个文件描述符中接受输入.
read -p 可以省略 echo -n 的提问
#!/bin/bash
echo -n "Enter your name: "
read name
echo "Hello $name!"
read -p "Enter your age: " age
echo "The age is $age"
read -t 可以让输入等待具体的几秒,然后退出。如果是超时退出,read指令会返回一个非零退出状态码。
#!/bin/bash
read -t 5 -p "Enter your height and weight: " height weight
if [ 0 -ne $? ]; then
echo
echo "Waiting for more than 5 secs"
else
echo
echo "His height and weight is $height and $weight"
fi
read -n1 告诉read命令在接受单个字符后退出。 也可以 -n2 -n3。。。
#!/bin/bash
read -n1 -p "Do you want to continue[Y/N]?" answer
case $answer in
Y | y) echo
echo "Ok, Let us keep going...";;
N | n) echo
echo "Fine, byebye";;
esac
read -s 可以让输入的字符不显示在屏幕上
#!/bin/bash
read -s -p "Enter your password: " password
echo
echo "The password is $password"
58. read命令 从文件读取
常见的方法是对文件使用cat命令,将结果通过管道直接传给含有read命令的while命令
#!/bin/bash
count=1
cat test1.txt | while read line
do
echo "Line $count: $line"
count=$[ $count + 1]
done
echo "Finished processing the file"
59. STDIN STDOUT STDERR
Linux系统里,默认情况下,0,1,2 这三个文件描述符已经被系统占用:
STDIN,标准输入,默认指向键盘;
STDOUT,标准输出,默认指向显示器;
STDERR,标准错误,默认指向显示器;
可以在命令行里,重定向输出流:1> obj 2> obj &> obj
cjh@cjh-PC:~/Shell_Programs$ ls test23.sh test35.sh -l 1> noteOK 2> noteERR &> notALL
cjh@cjh-PC:~/Shell_Programs$
noteOK:
-rwxr--r-- 1 cjh cjh 126 5月 11 11:39 test23.sh
noteERR:
ls: 无法访问 'test35.sh': 没有那个文件或目录
notALL: ( 同时接收 STDOUT STDERR )
ls: 无法访问 'test35.sh': 没有那个文件或目录
-rwxr--r-- 1 cjh cjh 126 5月 11 11:39 test23.sh
60. 脚本中重定向输出 exec 1>> >&1 >&2
#!/bin/bash
# testing STDERR messages
exec 2>> errlog2 # 将STDERR (附加)重定向到 errlog2文件
exec 1> notebook # 将STDOUT (覆盖)重定向到 notebook文件
echo "haha "
echo "how are u baby"
echo "I am wrong!" >&2 # 声明这句echo 应该定向到 STDERR
则屏幕上没有显示,输出语句都在 errlog2 和 notebook里面
61. 脚本中创建自定义文件描述符 exec x> obj
#!/bin/bash
# testing STDERR messages
exec 3>> mybook3
exec 4> mybook4
echo "abcde" >&3
echo "ersssdf" >&4
类似于用 3和4 作为fd,新建2个文件来分别保存不同种类的echo信息
62. 脚本中重定向输入 exec 0< obj
#!/bin/bash
# redirecting file input
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
用文件 testfile 的内容,来代替键盘输入。类似于58的方法。
63. 垃圾桶 /dev/null
在Linux系统,重定向到/dev/null的任何数据都会被丢掉,不会显示。也可以在输入重定向中将/dev/null作为输入文件,用它来快速清除现有文件中的数据。
ls -al > /dev/null
cat /dev/null > testfile
64. 创建临时文件或文件夹 mktemp
mktemp命令的输出, 正是它所创建的文件的名字。可以用好这一特性:
#!/bin/bash
# creating and using a temp file
tempfile=$(mktemp test19.XXXXXX)
exec 3>$tempfile # > 后面有无空格都可以
#!/bin/bash
# using a temporary directory
tempdir=$(mktemp -d dir.XXXXXX)
cd $tempdir
tempfile1=$(mktemp temp.XXXXXX)
tempfile2=$(mktemp temp.XXXXXX)
exec 7> $tempfile1
exec 8> $tempfile2
echo "Sending data to directory $tempdir"
echo "This is a test line of data for $tempfile1" >&7
echo "This is a test line of data for $tempfile2" >&8
65. T型接头 tee (-a)
tee命令相当于管道的一个T型接头。它将从STDIN过来的数据同时发往两处。一处是STDOUT,另一处是tee命令行所指定的文件名
#!/bin/bash
# using the tee command for logging
tempfile=test22file
echo "This is the start of the test" | tee $tempfile # 保存于tempfile
echo "This is the second line of the test" | tee -a $tempfile # 附加保存于tempfile
echo "This is the end of the test" | tee -a $tempfile # 附加保存于tempfile
test22file 文件也生成了。