Shell | Shell 的输入输出

这篇博客介绍了Shell脚本中的两种主要输出方法——echo和printf,其中echo支持转义和不换行,printf则能进行格式化输出且不自动换行。同时,文章讲解了Shell的输入方式,包括read命令及其参数如-p、-n和-t,以及通过命令行参数传递的方式。还提到了Shell变量的相关知识,如$0表示脚本名字,$#表示参数个数,$*表示所有参数,$$表示当前Shell进程ID,并提及shift命令用于参数移位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shell的输出:

        Shell的输出有两种常用方法:echo和printf

 

    echo和printf都是标准输出


 

    echo 的用法:

    不加参数,

    echo 的 -e 参数开启转义;\n 换行 \c 不换行

   

[root@localhost shell_protest]# sh output.sh abc\nabcabc[root@localhost shell_protest]# ==============Source=========================[root@localhost shell_protest]# cat output.sh#!/bin/bashecho "abc\n"echo -e "abc\n"echo -e "abc\c"

 

printf输出:

    printf不自带换行,可以多种格式输出

[root@localhost shell_protest]# sh printf.sh 1 abc1 abcabcdefabcdefa b cd e fg h ij   and 0  -------------------------------- [root@localhost shell_protest]# cat printf.sh#!/bin/bashprintf "%d %s\n" 1 "abc"# 没有引号也可以输出printf '%d %s\n' 1 "abc" #合并输出了printf %s abc def#循环输出,直至参数结束printf "%s\n" abc defprintf "%s %s %s\n" a b c d e f g h i j#没有输入时,%s输出是空格,%d输出的是0printf "%s and %d \n" 

 


 

Shell的输入:

        Shell的输入有两种常见方法:read $参数传递


 

    read的方法:

        read方法是从标准输入中获得一个输入,存放到一个标准变量中。

        介绍几个常用的参数:

        -p(提示语句)

        -n(字符个数)

        -t(等待时间)

        -s(不回显)

 

    -p作用:免去使用echo来提示,直接合成一个指令:

示例:

[root@localhost shell_protest]# sh ./input.sh input something:nickhello,nick[root@localhost shell_protest]# cat input.sh #!/bin/bash#shift 1#echo $1 $2#echo $0#echo $##echo $*read -p "input something:" name echo "hello,$name"

   -n:控制输入的有效字数:

[root@localhost shell_protest]# cat input.sh #!/bin/bashread -n 5 -p "input something:" name echo "hello,$name"[root@localhost shell_protest]# echo aaaaaaaaa|sh ./input.sh hello,aaaaa

 -t:控制超时时间,防止长时间没输入处于阻塞    

[root@localhost shell_protest]# sh input.sh input something:1hello,1##!在执行之后没有输入,自动跳过了输入!![root@localhost shell_protest]# cat input.sh#!/bin/bashread -t 3 naecho "$na"read -n 5 -p "input something:" name echo "hello,$name"

-s:不回显,一般用于密码输入

[root@localhost shell_protest]# sh input.sh 1233[root@localhost shell_protest]# cat input.sh #!/bin/bashread  -s  naecho "$na"

 


 

参数传递的方法,就是通过执行前,传递参数给程序

    用$n(n为自然数,从1~9),举个例子

    

[root@localhost shell_protest]# cat input.sh #!/bin/bashecho $1 $2[root@localhost shell_protest]# sh ./input.sh aa aaa a

 

顺带介绍:

    $0是脚本的名字。

    $#是传递给脚本或函数的参数个数。

    $*是传给脚本的全部参数。

    $$当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。

[root@localhost shell_protest]# cat  ./input.sh #!/bin/bashshift 1echo $1 $2echo $0echo $#echo $*----------------------------------------------[root@localhost shell_protest]# sh ./input.sh aa aa./input.sh1a===========================去掉shitf 1后==================[root@localhost shell_protest]# cat ./input.sh #!/bin/bash#shift 1echo $1 $2echo $0echo $#echo $*----------------------------------------------------[root@localhost shell_protest]# sh ./input.sh aa aaa a./input.sh2aa a

可以观察发现:shift 命令,是对传输参数的移位,把$1的值移到$2。从而影响了$#和$*的输出结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值