set使用命令替换功能把位置参数设置为ls -l的输出:
ashin@linux:~/test$ ls -l if3.sh -rwxrwxr-x 1 ashin ashin 490 1月 22 18:16 if3.sh ashin@linux:~/test$ echo $1 ashin@linux:~/test$ set -- $(ls -l if3.sh ) #因为有-l参数,所以使用-- ashin@linux:~/test$ echo $1 -rwxrwxr-x ashin@linux:~/test$ echo $2 1
只有硬链接文件的索引节点编号与原文件相同,软链接不同:
ashin@linux:~/test$ ls -i
4067127 bbbln.py 4067156 charity.jpg 4068071 if3.sh
4068759 bbblns.py 4068038 django_test 4067099 loginweibo.py
4067127 bbb.py 4066581 file 4067018 passwd.txt
查找指定索引节点编号的文件:
ashin@linux:~/test$ find . -xdev -inum 4067127 -print
./bbbln.py
./bbb.py
-xdev:cross-device防止find命令搜索其他文件系统上的目录
数组
ashin@linux:~/test$ name=(1 12 123 1234 12345) ashin@linux:~/test$ echo ${name[1]} 12 ashin@linux:~/test$ echo ${#name[1]} 2 ashin@linux:~/test$ echo ${name[*]} 1 12 123 1234 12345 ashin@linux:~/test$ echo ${name[@]} 1 12 123 1234 12345 ashin@linux:~/test$ echo ${#name[@]} 5 ashin@linux:~/test$ echo ${#name[*]} 5 ashin@linux:~/test$ name[0]=xx ashin@linux:~/test$ echo ${name[0]} xx ashin@linux:~/test$ a=("${name[*]}") ashin@linux:~/test$ echo $a xx 12 123 1234 12345 ashin@linux:~/test$ b=("${name[@]}") ashin@linux:~/test$ echo $b xx ashin@linux:~/test$ declare -a declare -a a='([0]="xx 12 123 1234 12345")' declare -a b='([0]="xx" [1]="12" [2]="123" [3]="1234" [4]="12345")' declare -a name='([0]="xx" [1]="12" [2]="123" [3]="1234" [4]="12345")'
typeset定义局部变量
ashin@linux:~/test$ function down(){ > typeset count > count=$1 > while [ $count -gt 0 ] > do > echo "$count..." > ((count-=1)) > sleep 1 > done > echo 'over' > } ashin@linux:~/test$ count=10 ashin@linux:~/test$ down 5 5... 4... 3... 2... 1... over ashin@linux:~/test$ echo $count 10
如果没有typeset的话,最后echo的count为0
特殊参数
ashin@linux:~/test$ echo $$ #PID编号 2445 ashin@linux:~/test$ echo $! #后台运行的最后一个进程的PID编号 ashin@linux:~/test$ sleep 100 & [1] 2988 ashin@linux:~/test$ echo $! 2988 ashin@linux:~/test$ sleep 100 & [2] 2989 ashin@linux:~/test$ sleep 100 & [3] 2990 ashin@linux:~/test$ echo $! 2990 ashin@linux:~/test$ ls . bbbln.py.tt.py.tt.py django_test.tt.py.tt.py loginweibo.py.tt.py.tt.py bbblns.py.tt.py.tt.py file.tt.py.tt.py music.txt bbb.py.tt.py.tt.py if3.sh.tt.py.tt.py passwd.txt.tt.py.tt.py charity.jpg.tt.py.tt.py locktty.sh tt.sh ashin@linux:~/test$ echo $? #退出状态 0 ashin@linux:~/test$ ls ./aa ls: 无法访问./aa: 没有那个文件或目录 ashin@linux:~/test$ echo $? 2
位置参数:
$1-$n:命令行参数,超过9用{}包起来,${12} set:初始化命令行参数 shift:左移命令行参数,最左边的参数被丢弃,第二个参数变为第一个参数。 ashin@linux:~/test$ cat shift_t.sh #!/bin/bash echo "arg1=$1 arg2=$2 arg3=$3" shift echo "arg1=$1 arg2=$2 arg3=$3" shift echo "arg1=$1 arg2=$2 arg3=$3" shift echo "arg1=$1 arg2=$2 arg3=$3" shift ashin@linux:~/test$ ./shift_t.sh aa bb cc arg1=aa arg2=bb arg3=cc arg1=bb arg2=cc arg3= arg1=cc arg2= arg3= arg1= arg2= arg3=
type显示命令的相关信息
ashin@linux:~/test$ type cat echo grep
cat 是 /bin/cat
echo 是 shell 内嵌
grep 是 `grep --color=auto' 的别名
shell注入:
ashin@linux:~/test$ cat read1 #!/bin/bash echo -n "input :" read firstline echo u inputed: $firstline ashin@linux:~/test$ ./read1 input :* u inputed: bbbln.py.tt.py.tt.py bbblns.py.tt.py.tt.py bbb.py.tt.py.tt.py charity.jpg.tt.py.tt.py django_test.tt.py.tt.py file.tt.py.tt.py if3.sh.tt.py.tt.py locktty.sh loginweibo.py.tt.py.tt.py music.txt passwd.txt.tt.py.tt.py read1 shift_t.sh ttsc tt.sh typescript ashin@linux:~/test$
变量应该被双引号引起来,单引号不会对$表示的变量进行转义。
stty -echo将不显示输入的值
stty echo重新显示
read -s参数也不回显输入值
read -sp "input words:" words
trap捕获信号并执行指定命令
ashin@linux:~/test$ trap 'echo -en "\n ----contral+c ing"' INT ashin@linux:~/test$ ^C ----contral+c ing ashin@linux:~/test$
网友185.*.*.38[火星]2022-05-26 22:25
网友40.*.*.46[美国]2022-05-26 22:14
网友66.*.*.212[火星]2022-05-26 22:03
网友185.*.*.40[火星]2022-05-26 22:01
发表评论
亲~ 评论内容是必须的哟! o(∩_∩)o
昵称
邮箱
主页
评论