30分钟学习shell常用命令
命令
cat /etc/shells
echo $PATH
echo $SHELL
echo $0 查看当前执行的脚本
可以编写自动化任务,shell脚本
vi hello.sh
#!/bin/bash
echo "hello shell"
date
whoami
添加权限 chmod +x hello.sh
运行:
sh hello.sh
编程
vi game.sh
猜数字小游戏
#!/bin/bash
echo "input your name:"
#read name
name=$1
channel=$2
echo "hello, $name,welcome to $channel"
使用 bash game.sh 执行
可以使用 echo $name 定义变量,使用 export name=gao 设置为环境变量。重新登录系统后变量值会变为空
*所有的linux文件的隐藏文件都是以.开头
ls /etc/bash
nano .bashrc
#定义参数
Shuf/if/while/random用法
#!/bin/bash
echo "input your name:"
#read name
name=$1
channel=$2
echo "hello, $name,welcome to $channel"
number=$(shuf -i 1-10 -n 1)
echo $number
#while [[ $guess -ne $number ]]
while true
do
echo "please input a number:"
read guess
if [[$guess -eq $number ]]; then
echo "right,continue?(y/n):"
read choice
if [[$choice = "y"]] || [[$choice = "Y"]]; then
number=$((RANDOM % 10 + 1))
echo $number
continue
else
break
fi
elif [[$guess -lt $number]] ; then
echo "little"
else
echo "big"
fi
done

浙公网安备 33010602011771号