Shell echo命令

Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出。命令格式:

1.显示普通字符串:
  echo "It is a test"
这里的双引号完全可以省略,以下命令与上面实例效果一致:
  echo It is a test

2.read的使用

  • -p 输入提示文字
  •  -n 输入字符长度限制(达到6位,自动结束)
  •  -t 输入限时
  •  -s 隐藏输入内容

测试文件 test.sh 代码如下:

read firstStr secondStr
echo "第一个参数:$firstStr; 第二个参数:$secondStr"

执行测试:

$ sh test.sh 
   
第一个参数:一; 第二个参数:二  


3.显示结果写入至文件

echo "It is a test"  >> myfile

#!/bin/bash
echo ----------------Example for echo and printf useing ----------------------------
echo `date`
read -p "pls enter name & passwod: " -n 50 -t 10  name  password
echo `date`
#把输入的数据保存到logs.txt中
echo -e "your name:$name password:$password; \n" >> ./logs.txt
echo "'this is a string' string's \"ABCDE\""

运行如下:

daokr@DK:~/myfile$ ./inout.sh 
----------------Example for echo and printf useing ----------------------------
2018年 03月 30日 星期五 17:08:45 CST
pls enter name & passwod: wangg 123456
2018年 03月 30日 星期五 17:08:49 CST
'this is a string' string's "ABCDE"

logs.txt 文件内容

daokr@DK:~/myfile$ tailf logs.txt 
your name:wl password:jj; 

 

posted @ 2018-03-30 17:14  王默默  阅读(4466)  评论(0编辑  收藏  举报