如何创建shell脚本并执行

使用编辑器创建shell脚本vim name.sh

 #!/bin/bash
#This script displays the date and who's logged on

输入要执行的命令

 将文本保存并退出

在命令行中执行./dirname,遇到permission denied需要添加文件权限

chmod u+x dirname

再次执行 ./dirname即可运行shell脚本文件 

​
#!/bin/bash
#This script displays the date and who's logged on
echo "Hello World!"
echo the comment 'echo' shows the Strings.
echo "the comment 'echo' shows the Strings."
echo -n "the time and date are:"
date
echo "Let's see who's logged into the system:"
who #who命令无输出结果可以执行whoami
sudo useradd -d /usr/zzh -m zzh
#!/bin/bash
#This script displays usr information from the system.
echo "User info for userid: $USER"
echo UID:$UID
echo HOME:$HOME
echo "The cost of the item is $15."
echo "The cost of the item is \$15."
#!/bin/bash
#This script displays user variable.
guest="lys"
days1=5
echo "$guest checked in $days1 days ago."
guest="jerry"
days2=10
echo "$guest checked in $days2 days ago."
days2=$days1
echo "$guest checked in $days2 days ago."
#!/bin/bash
#This script displays the date and who's logged on
out_date='date'
echo"The time and date are:"$out_date
out_user=$(who)
echo "Let's see who's logged into the system:"$out_user
#copy the /usr/bin directory listing to a log file
today=$(date +%y%m%d)
ls /usr/bin -al >log.$today

#!/bin/bash
#This script displays the dc expression
var1=$(echo "scale=5; 3.44 /5" | bc)
echo The answer is $var1

var1=100
var2=45
var3=$(echo "scale=3;$var1 / $var2" | bd)
echo THe answer for this is $var3

var1=10.46
var2=43.67
var3=33.2
var4=71

var5=$(bc <<EOF
scale = 4
a1 = ($var1 * $var2)
b1 = ($var3 * $var4)
a1 + b1
EOF
)
echo The final answer us $var5
var=5
exit $var

                         

​

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值