linux shell 入门学习笔记10内置shell命令

bash基础的内置命令
  • echo
  • eval
  • exec
  • export
  • read
  • shift

echo命令
-n 不换行输出
-e 解析字符串中的特殊符号

\n 换行
\r 回车
\t 制表符 四个空格
\b 退格

-n参数演示

xiao123@xiao123:~/Downloads$ echo 你真胖;echo 你还挺可爱;
你真胖
你还挺可爱
xiao123@xiao123:~/Downloads$ echo -n 你真胖;echo 你还挺可爱;
你真胖你还挺可爱
xiao123@xiao123:~/Downloads$ echo -n 你真胖;echo -n 你还挺可爱;
你真胖你还挺可爱xiao123@xiao123:~/Downloads$

-e参数演示

xiao123@xiao123:~/Downloads$ echo "我看你挺\n好的"
我看你挺\n好的
xiao123@xiao123:~/Downloads$ echo -e "我看你挺\n好的"
我看你挺
好的
xiao123@xiao123:~/Downloads$ printf "我看你挺\n好的"
我看你挺
好的xiao123@xiao123:~/Downloads$ printf "我看你挺\n好的\n"
我看你挺
好的
xiao123@xiao123:~/Downloads$
eval命令

执行多个命令

xiao123@xiao123:~/Downloads$ eval ls;cd /tmp
different.sh  make_vars.sh  nohup.out  special_test.sh  t1.sh  test.sh
xiao123@xiao123:/tmp$
exec命令

不创建子进程,执行后续命令,且执行完毕后,自动exit。

xiao123@xiao123:~/Downloads$ su - root
Password:
root@xiao123:~# exec date
2023年 02月 25日 星期六 10:45:02 CST
xiao123@xiao123:~/Downloads$
export命令

查找和设置环境变量的值

read 命令

用于接收标准输入或者其他文件描述符的输入,并且可以和用户进行交互。如果没有指定变量名,读取的数据将被自动赋值给特定的变量$REPLY。read每次调用读取一行。

-a	指定变量为数组
-r	反斜杠转义不会生效,意味着行末的’\’成为有效的字符,例如使 \n 成为有效字符而不是换行
-p	指定输出提示信息
-d	输入结束符,当输入的内容出现这个字符时,立即结束。一般情况下是以IFS为参数的间隔,但是通过-d自定义
-n	指定输入的字符长度
-t	指定读取值时等待的时间(秒),read命令会一直等待用户输入,时间到自动退出
-s	不显示输入的值,一般用于密码
IFS (Internal Field Separator)输入字段分隔符,Bash 会根据 IFS 中定义的字符来进行字符串拆分。

无选项单参数
不带参数,输入值都用空格隔开

xiao123@xiao123:~/Downloads$ read ARGS
11 22 33
xiao123@xiao123:~/Downloads$ echo ${ARGS}
11 22 33
xiao123@xiao123:~/Downloads$

无选项多参数
定义多个变量,变量和输入值都用空格隔开。

xiao123@xiao123:~/Downloads$ read ARG1 ARG2
11 22 33 44
xiao123@xiao123:~/Downloads$ echo ${ARG1} ${ARG2}
11 22 33 44
xiao123@xiao123:~/Downloads$ echo ${ARG1}
11
xiao123@xiao123:~/Downloads$ echo ${ARG2}
22 33 44
xiao123@xiao123:~/Downloads$

-a展示
将接收到的数据看作一个数组。使用IFS定义的分割符分割。

xiao123@xiao123:~/Downloads$ read -a ARGS
11 22 33 44 55
xiao123@xiao123:~/Downloads$ echo ${ARGS[*]}
11 22 33 44 55
xiao123@xiao123:~/Downloads$ echo ${ARGS[1]} ${ARGS[2]}
22 33
xiao123@xiao123:~/Downloads$

-r展示
字符串原样输出,不转义特殊字符。

xiao123@xiao123:~/Downloads$ read -r ARGS
\b\narg
xiao123@xiao123:~/Downloads$ echo ${ARGS}
\b\narg
xiao123@xiao123:~/Downloads$

-d展示
指定read命令的结束符。

xiao123@xiao123:~/Downloads$ read -d "-" ARG
arg1
arg2
arg3
-xiao123@xiao123:~/Downloads$ echo ${ARG}
arg1 arg2 arg3
xiao123@xiao123:~/Downloads$

-p展示
指定输出提示信息。

xiao123@xiao123:~/Downloads$ read -p "请输入一个字符:" ARG
请输入一个字符:a
xiao123@xiao123:~/Downloads$ echo ${ARG}
a
xiao123@xiao123:~/Downloads$

-t展示
指定输入的超时时间。单位:s

xiao123@xiao123:~/Downloads$ read -t 3 ARG
xiao123@xiao123:~/Downloads$

-s展示
不显示输入的字符。

xiao123@xiao123:~/Downloads$ read -s ARG
xiao123@xiao123:~/Downloads$ echo ${ARG}
1234567890
xiao123@xiao123:~/Downloads$

IFS展示

xiao123@xiao123:~/Downloads/shscripts$ # 打印IFS的值
xiao123@xiao123:~/Downloads/shscripts$ echo -n "${IFS}" | hexdump -C
00000000  20 09 0a                                          | ..|
00000003
xiao123@xiao123:~/Downloads/shscripts$ # 可以看出IFS的默认值为" /t/n"
xiao123@xiao123:~/Downloads$ IFS='|'
xiao123@xiao123:~/Downloads$ read ARG1 ARG2 ARG3
1|2|3
xiao123@xiao123:~/Downloads$ echo ${ARG1}
1
xiao123@xiao123:~/Downloads$ echo ${ARG2}
2
xiao123@xiao123:~/Downloads$ echo ${ARG3}
3
xiao123@xiao123:~/Downloads$

IFS文件实验展示
注意使用通配符*会出现意想不到问题:
异场情况1:

xiao123@xiao123:~/Downloads/shscripts$ cat ./ifs.sh
#!/bin/bash

str="C,BASH,*"

IFS=',' read -ra aar <<< ${str}

echo ${#aar[@]}
echo ${aar[@]}

echo "ls:"
ls
xiao123@xiao123:~/Downloads/shscripts$ ./ifs.sh
3
C BASH calculation.sh chaochao_1_finished.png chaochao_1.jpg chaochao_2_finished.png chaochao_2.jpg chaochao_3_finished.png chaochao_3.jpg chaochao_4_finished.png chaochao_4.jpg chaochao_5_finished.png chaochao_5.jpg check_url.sh del_data.sh expr_test1.sh expr_test.sh free_1.sh fun3.sh func1.sh hello if_1.sh if_read.sh ifs.sh index.html index.html.1 index.html.10 index.html.11 index.html.12 index.html.13 index.html.14 index.html.15 index.html.16 index.html.17 index.html.18 index.html.19 index.html.2 index.html.20 index.html.21 index.html.22 index.html.23 index.html.24 index.html.25 index.html.26 index.html.27 index.html.28 index.html.29 index.html.3 index.html.30 index.html.31 index.html.32 index.html.33 index.html.4 index.html.5 index.html.6 index.html.7 index.html.8 index.html.9 let_test.sh my_function.sh mysql_test.php mysql_test.py rsyncing2.sh rsyncing.sh test test_input.sh test_install.sh 鸡你太美.jpg
ls:
calculation.sh           chaochao_5.jpg  if_read.sh     index.html.16  index.html.25  index.html.4    rsyncing2.sh
chaochao_1_finished.png  check_url.sh    ifs.sh         index.html.17  index.html.26  index.html.5    rsyncing.sh
chaochao_1.jpg           del_data.sh     index.html     index.html.18  index.html.27  index.html.6    test
chaochao_2_finished.png  expr_test1.sh   index.html.1   index.html.19  index.html.28  index.html.7    test_input.sh
chaochao_2.jpg           expr_test.sh    index.html.10  index.html.2   index.html.29  index.html.8    test_install.sh
chaochao_3_finished.png  free_1.sh       index.html.11  index.html.20  index.html.3   index.html.9    鸡你太美.jpg
chaochao_3.jpg           fun3.sh         index.html.12  index.html.21  index.html.30  let_test.sh
chaochao_4_finished.png  func1.sh        index.html.13  index.html.22  index.html.31  my_function.sh
chaochao_4.jpg           hello           index.html.14  index.html.23  index.html.32  mysql_test.php
chaochao_5_finished.png  if_1.sh         index.html.15  index.html.24  index.html.33  mysql_test.py
xiao123@xiao123:~/Downloads/shscripts$

异场情况2:

xiao123@xiao123:~/Downloads/shscripts$ cat ./cut.sh
#! /bin/bash

str="C,BASH,*"

arr=(${str//,/ })

echo ${#arr[@]}
echo ${arr[@]}
xiao123@xiao123:~/Downloads/shscripts$ ./cut.sh
69
C BASH calculation.sh chaochao_1_finished.png chaochao_1.jpg chaochao_2_finished.png chaochao_2.jpg chaochao_3_finished.png chaochao_3.jpg chaochao_4_finished.png chaochao_4.jpg chaochao_5_finished.png chaochao_5.jpg check_url.sh cut.sh del_data.sh expr_test1.sh expr_test.sh free_1.sh fun3.sh func1.sh hello if_1.sh if_read.sh ifs.sh index.html index.html.1 index.html.10 index.html.11 index.html.12 index.html.13 index.html.14 index.html.15 index.html.16 index.html.17 index.html.18 index.html.19 index.html.2 index.html.20 index.html.21 index.html.22 index.html.23 index.html.24 index.html.25 index.html.26 index.html.27 index.html.28 index.html.29 index.html.3 index.html.30 index.html.31 index.html.32 index.html.33 index.html.4 index.html.5 index.html.6 index.html.7 index.html.8 index.html.9 let_test.sh my_function.sh mysql_test.php mysql_test.py rsyncing2.sh rsyncing.sh test test_input.sh test_install.sh 鸡你太美.jpg
xiao123@xiao123:~/Downloads/shscripts$

正常情况:

xiao123@xiao123:~/Downloads$ cat ./file.csv

cyberciti.biz|202.54.1.1|/home/httpd|ftpcbzuser
nixcraft.com|202.54.1.2|/home/httpd|ftpnixuser
xiao123@xiao123:~/Downloads$ cat ./ifstest.sh
file=./file.csv

IFS='|'
printf "domain\t\tip\t\twebroot\t\tusername\n"
while read -r domain ip webroot username;
do
        printf "%s\t%s\t%s\t%s\n" ${domain} ${ip} ${webroot} ${username}
done < "${file}"
xiao123@xiao123:~/Downloads$ bash ./ifstest.sh
domain          ip              webroot         username

cyberciti.biz   202.54.1.1      /home/httpd     ftpcbzuser
nixcraft.com    202.54.1.2      /home/httpd     ftpnixuser
xiao123@xiao123:~/Downloads$
shift命令

移动命令行参数,默认命令shift=shift 1
shift例子1

xiao123@xiao123:~/Downloads$ cat shift.sh
echo ">>没有使用shift"
echo "输入的参数数量: $#"
echo '$1 $2 $3是:' $1, $2, $3
shift 2
echo ">>使用了shift2之后"
echo "剩余输入的参数数量:$#"
echo '$1 $2 $3是:' $1, $2, $3
xiao123@xiao123:~/Downloads$ bash ./shift.sh 11 22 33
>>没有使用shift
输入的参数数量: 3
$1 $2 $3是: 11, 22, 33
>>使用了shift2之后
剩余输入的参数数量:1
$1 $2 $3是: 33, ,
xiao123@xiao123:~/Downloads$

shift例子2

xiao123@xiao123:~/Downloads$ cat ./shift2.sh
until [ -z "$1" ]
do
        echo "$@"
        shift
done
xiao123@xiao123:~/Downloads$ bash ./shift2.sh 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
xiao123@xiao123:~/Downloads$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值