William126

从零开始学Shell(二)

$1,$2....${10},${11},
[root@localhost cee]# cat p.sh
#!/bin/bash
#file_name:p.sh
echo $1 $2
[root@localhost cee]# sh p.sh 123 william
123 william
[root@localhost cee]# sh p.sh 123 william 456
123 william
[root@localhost cee]#

[root@localhost cee]# echo \${1..15}
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[root@localhost cee]# echo \${1..15} > n.sh
[root@localhost cee]# cat n.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[root@localhost cee]# cat n.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
[root@localhost 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
[root@localhost cee]# sh n.sh {a..z}
a b c d e f g h i a0 a1 a2 a3 a4 a5
[root@localhost cee]# cat n.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
[root@localhost cee]# sh n.sh {a..z}
a b c d e f g h i j k l m n o
[root@localhost cee]# cat q.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[root@localhost cee]# sh q.sh {a..z}
a b c d e f g h i
26

[root@localhost cee]# cat t1.sh
[ $# -ne 2 ] && {
    echo "muse two args"
    exit 1
}

echo william
[root@localhost cee]# sh t1.sh
muse two args
[root@localhost cee]# sh t1.sh 234
muse two args
[root@localhost cee]# sh t1.sh 234 fds
william

[root@localhost cee]# cat t2.sh
if [ $# -ne 2 ];then
    echo "USAGE:/bin/sh $0 arg1 arg2"
    exit 1
fi
echo $1 $2
[root@localhost cee]# sh t2.sh
USAGE:/bin/sh t2.sh arg1 arg2
[root@localhost cee]# sh t2.sh arg1
USAGE:/bin/sh t2.sh arg1 arg2
[root@localhost cee]# sh t2.sh arg1 arg2
arg1 arg2

[root@localhost cee]# set -- "I am" handsome oldboy
[root@localhost cee]# echo $#
3
[root@localhost cee]# echo $1
I am
[root@localhost cee]# echo $2
handsome
[root@localhost cee]# echo $3
oldboy
[root@localhost cee]# echo $4

[root@localhost cee]# echo $4$*
I am handsome oldboy
[root@localhost cee]# echo $*
I am handsome oldboy
[root@localhost cee]# echo $@
I am handsome oldboy
[root@localhost cee]# for i in $*;do echo $i;done;
I
am
handsome
oldboy
[root@localhost cee]# for i in $@;do echo $i;done;
I
am
handsome
oldboy
[root@localhost cee]# echo "$*"
I am handsome oldboy
[root@localhost cee]# echo "$@"
I am handsome oldboy
[root@localhost cee]# for i in "$*";do echo $i;done;
I am handsome oldboy
[root@localhost cee]# for i in "$@";do echo $i;done;
I am
handsome
oldboy
[root@localhost cee]# for i;do echo $i;done;
I am
handsome
oldboy
[root@localhost cee]# echo $?   #上一次命令的运行结果,0为运行成功,非0为运行不成功;
0
[root@localhost cee]# sed -n '13,14p' /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[root@localhost 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
[root@localhost cee]# echo "oldboy\toldgirl\t"
oldboy\toldgirl\t
[root@localhost cee]# echo -e "oldboy\toldgirl\t"
oldboy    oldgirl    
[root@localhost cee]# cat noeval.sh
echo \$$#
[root@localhost cee]# cat eval.sh
eval "echo \$$#"
[root@localhost cee]# sh noeval.sh args args2
$2
[root@localhost cee]# sh eval.sh args args2
args2
[root@localhost ~]# cat /tmp/tmp.log
1
2
3
4
5
[root@localhost ~]# vim exec.sh
[root@localhost ~]# cat exec.sh
exec </tmp/tmp.log
while read line
    do
        echo $line
    done
    echo OK
[root@localhost ~]# sh exec.sh
1
2
3
4
5
OK
[root@localhost ~]# cat shift.sh
echo $1 $2
if [ $# -ne 2 ];then
    shift
    echo $1
fi
[root@localhost ~]# sh shift.sh 1 2
1 2
[root@localhost ~]# sh shift.sh 1
1

posted on 2019-02-20 11:51  William126  阅读(135)  评论(0编辑  收藏  举报

导航