shell之eval

功能

告知shell读取一连串的参数,然后再依照参数本身的特性来执行。

举例

  1. 变量两次扫描
tom@tom-linuxer:~$ cat file1.sh
#!/bin/bash
echo "This is a file1"
tom@tom-linuxer:~$ myfile="cat file1.sh"
tom@tom-linuxer:~$ eval $myfile
#!/bin/bash
echo "This is a file1"

2. 打印指定参数

tom@tom-linuxer:~$ cat test.sh 
#!/bin/bash
echo $*
echo $@
echo $#
echo $2
eval echo \$$#
tom@tom-linuxer:~$ ./test.sh 3 5 6 27
3 5 6 27
3 5 6 27
4
5
27

说明:
$#: 是参数个数
$N: 输入第N个参数

第一扫描,则转换$#为4,然后在打印$4,第二次扫描结果是27。
转译$确保,第一个$不会被其他组合。

发布了112 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chengbeng1745/article/details/103101105