平时用到的,从网上查来的教程,自己码的字。
bash 延时指令sleep的用法
#!/bin/bash
echo “请输入求和的第一个数字并按回车”
read a
echo “请输入求和的第二个数字并按回车”
read b
echo “请输入求和的第三个数字并按回车”
read c
let d=$a+$b+$c
sleep $a\s
echo “求和的结果是”$d
这里面倒数第二行的s表示秒,还可以换成m表示分钟,h表示小时,d表示天。
bash 脚本自动输入密码的方法
第一种方法
使用管道(上一个命令的 stdout 接到下一个命令的 stdin):
#!/bin/bash
echo password | sudo -S apt-get update
第二种方法
使用文本块输入重定向:
#!/bin/bash
sudo -S apt-get update « EOF
你的密码
EOF
说明
在shell脚本中,通常将EOF与 « 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主Shell,即将‘你的密码’当做命令的输入 -S 参数的作用
使用man命令查询sudo,对参数-S的说明如下:
–stdin: Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.