从零开始学Shell(二)
Posted William126
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从零开始学Shell(二)相关的知识,希望对你有一定的参考价值。
$1,$2....${10},${11},
[[email protected] cee]# cat p.sh
#!/bin/bash
#file_name:p.sh
echo $1 $2
[[email protected] cee]# sh p.sh 123 william
123 william
[[email protected] cee]# sh p.sh 123 william 456
123 william
[[email protected] cee]#
[[email protected] cee]# echo ${1..15}
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[[email protected] cee]# echo ${1..15} > n.sh
[[email protected] cee]# cat n.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[[email protected] cee]# cat n.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[[email protected] cee]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[[email protected] cee]# sh n.sh {a..z}
a b c d e f g h i a0 a1 a2 a3 a4 a5
[[email protected] cee]# cat n.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
[[email protected] cee]# sh n.sh {a..z}
a b c d e f g h i j k l m n o
[[email protected] cee]# cat q.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[[email protected] cee]# sh q.sh {a..z}
a b c d e f g h i
26
[[email protected] cee]# cat t1.sh
[ $# -ne 2 ] && {
echo "muse two args"
exit 1
}
echo william
[[email protected] cee]# sh t1.sh
muse two args
[[email protected] cee]# sh t1.sh 234
muse two args
[[email protected] cee]# sh t1.sh 234 fds
william
[[email protected] cee]# cat t2.sh
if [ $# -ne 2 ];then
echo "USAGE:/bin/sh $0 arg1 arg2"
exit 1
fi
echo $1 $2
[[email protected] cee]# sh t2.sh
USAGE:/bin/sh t2.sh arg1 arg2
[[email protected] cee]# sh t2.sh arg1
USAGE:/bin/sh t2.sh arg1 arg2
[[email protected] cee]# sh t2.sh arg1 arg2
arg1 arg2
[[email protected] cee]# set -- "I am" handsome oldboy
[[email protected] cee]# echo $#
3
[[email protected] cee]# echo $1
I am
[[email protected] cee]# echo $2
handsome
[[email protected] cee]# echo $3
oldboy
[[email protected] cee]# echo $4
[[email protected] cee]# echo $4$*
I am handsome oldboy
[[email protected] cee]# echo $*
I am handsome oldboy
[[email protected] cee]# echo [email protected]
I am handsome oldboy
[[email protected] cee]# for i in $*;do echo $i;done;
I
am
handsome
oldboy
[[email protected] cee]# for i in [email protected];do echo $i;done;
I
am
handsome
oldboy
[[email protected] cee]# echo "$*"
I am handsome oldboy
[[email protected] cee]# echo "[email protected]"
I am handsome oldboy
[[email protected] cee]# for i in "$*";do echo $i;done;
I am handsome oldboy
[[email protected] cee]# for i in "[email protected]";do echo $i;done;
I am
handsome
oldboy
[[email protected] cee]# for i;do echo $i;done;
I am
handsome
oldboy
[[email protected] cee]# echo $? #上一次命令的运行结果,0为运行成功,非0为运行不成功;
0
[[email protected] cee]# sed -n ‘13,14p‘ /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[[email protected] cee]# ps -ef | grep bash |grep -v grep
root 758 1 0 03:32 ? 00:00:00 /bin/bash /usr/sbin/ksmtuned
root 1692 1520 0 03:32 ? 00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
root 3270 3263 0 04:05 pts/0 00:00:01 bash
[[email protected] cee]# echo "oldboy oldgirl "
oldboy oldgirl
[[email protected] cee]# echo -e "oldboy oldgirl "
oldboy oldgirl
[[email protected] cee]# cat noeval.sh
echo $$#
[[email protected] cee]# cat eval.sh
eval "echo $$#"
[[email protected] cee]# sh noeval.sh args args2
$2
[[email protected] cee]# sh eval.sh args args2
args2
[[email protected] ~]# cat /tmp/tmp.log
1
2
3
4
5
[[email protected] ~]# vim exec.sh
[[email protected] ~]# cat exec.sh
exec </tmp/tmp.log
while read line
do
echo $line
done
echo OK
[[email protected] ~]# sh exec.sh
1
2
3
4
5
OK
[[email protected] ~]# cat shift.sh
echo $1 $2
if [ $# -ne 2 ];then
shift
echo $1
fi
[[email protected] ~]# sh shift.sh 1 2
1 2
[[email protected] ~]# sh shift.sh 1
1
以上是关于从零开始学Shell(二)的主要内容,如果未能解决你的问题,请参考以下文章