使用编辑器创建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