shell 学习笔记4-shell内置变量命令

一、shell 的一些内置命令

  常用的一内部命令有:echo、eval、exec、export、read、shift

  1、echo命令-在屏幕中输出信息

    1)说明

    格式:echo args #<== 可以是字符串和变量的组合

    说明:将echo命令后面args指定的字符串及变量等显示到标准输出

 

    2)、示例1

[root@web1 scripts]# echo zxg;echo shell        #<---默认换行了
zxg
shell
[root@web1 scripts]# echo -n  zxg;echo shell       #<---不换行用-n
zxgshell
[root@web1 scripts]# echo -e zxg\tshell\nzxg\tshell  #<---使用-e需要加""号,要不然无效
zxgtshellnzxgtshell
[root@web1 scripts]# echo -e "zxg\tshell\nzxg\tshell" #<---使用了\n \t
zxg     shell
zxg     shell
[root@web1 scripts]# printf "zxg\tshell\nzxg\tshell\n"  #<---printf用法
zxg     shell
zxg     shell
[root@web1 scripts]# echo -e "1\b23"
23
[root@web1 scripts]# printf  "1\b23\n"            #<---使用最多的还是echo,printf更强的大,只有在处理特殊复杂的适合才考虑
23
[root@web1 scripts]# 

  2、eval命令

    1)说明

    格式:eval args

    功能:当shell程序执行到eval语句时,shell读入参数args,并将他们组合成一个新的命令,然后执行

    2)示例1

[root@web1 scripts]# cat test12.sh 
#!/bin/bash
echo \$$#                      #<---表示传参的个数


[root@web1 scripts]# ./test12.sh arg1 arg2   
$2                          #<---传入两个参数,因此$#为2,于是echo \$$# 就变成echo $2,并没有打印arg2
[root@web1 scripts]# cat test12-1.sh 
#!/bin/bash
eval "echo \$$#"                  #<---加上eval命令,使得打印的特殊位置参数,重新解析输出,而不是输出$2本身


[root@web1 scripts]# ./test12-1.sh arg1 arg2
arg2                         #<---输出了$2
[root@web1 scripts]# 

  3、exec命令

    1)说明

    格式:exec 命令参数

    功能:exec命令能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该经常(最初的shell)就终止了

    2)示例1

root@web1 ~]# su zxg
[zxg@web1 root]$ 
[zxg@web1 root]$ 
[zxg@web1 root]$ 
[zxg@web1 root]$ 
[zxg@web1 root]$ exec date
Mon Jul  8 13:01:11 CST 2019
[root@web1 ~]# 

     3)当使用exec打开文件后,read命令每次都将文件指针移动到文件的下一行进行读取,知道文件末尾,利用这个可以实现处理文件内容

[root@web1 ~]# seq 5 >/tmp/tmp.log
[root@web1 scripts]# cat test13.sh 
#!/bin/bash
exec </tmp/tmp.log                    #<---读取log内容
while read line                      #<---利用read一行行读取处理
do
        echo $line                    #打印输出
done                              
echo ok

[root@web1 scripts]# chmod +x test13.sh
[root@web1 scripts]# ./test13.sh 
1
2
3
4
5
ok
[root@web1 scripts]# 

  4、read命令

    1)说明

    格式:read变量名表

    功能:从标准输入读取字符串等信息,传给shell程序内部定义的变量

    参数比较多如下:(后面单独写一篇博客,未完成)

选项说明:
-a:将分裂后的字段依次存储到指定的数组中,存储的起始位置从数组的index=0开始。
-d:指定读取行的结束符号。默认结束符号为换行符。
-n:限制读取N个字符就自动结束读取,如果没有读满N个字符就按下回车或遇到换行符,则也会结束读取。
-N:严格要求读满N个字符才自动结束读取,即使中途按下了回车或遇到了换行符也不结束。其中换行符或回车算一个字符。
-p:给出提示符。默认不支持"\n"换行,要换行需要特殊处理,见下文示例。例如,"-p 请输入密码:"
-r:禁止反斜线的转义功能。这意味着"\"会变成文本的一部分。
-s:静默模式。输入的内容不会回显在屏幕上。
-t:给出超时时间,在达到超时时间时,read退出并返回错误。也就是说不会读取任何内容,即使已经输入了一部分。
-u:从给定文件描述符(fd=N)中读取数据。

 

  5、shift命令

    1)说明

    格式:shift-Shift positional parameters

    功能:shift语句会按如下方式重新命名所有的位置参数变量,即$2成为$1、$3成为¥2,在程序中每使用一次shift语句,都会时所有的位置参数一次向左移动一个位置,并使位置参数$#减1,直到减到0为止

    2)示例1

[root@web1 scripts]# cat test14.sh 
#!/bin/bash
echo $1 $2
if [ $# -eq 2 ];then
        shift
        echo $1
fi

[root@web1 scripts]# chmod +x test14.sh
[root@web1 scripts]# ./test14.sh

[root@web1 scripts]# ./test14.sh 1 1
1 1
1
[root@web1 scripts]# ./test14.sh 1 2
1 2                        #<---这是echo $1 $2 的结果
2                         #<---这是echo $1的结果,但是输出的使传参时$2的值
[root@web1 scripts]# 

  6、exit命令

    1)说明

    格式:exit-Exit the shell

    功能:退出shell程序,在exit之后可以有选择

 

 

转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11150343.html 

posted @ 2019-07-09 15:53  乐章  阅读(766)  评论(0编辑  收藏  举报