Shell基本使用

shell说明

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

shell使用

第一个shell脚本

echo "Hello world"

可以创建test.sh文件,把上面的脚本写进去

touch test.sh
vi test.sh
echo "Hello world"

再执行./test.sh

分配权限再执行:

chmod 777 test.sh
./test.sh

变量

声明变量赋值

myname="coydone"
echo $myname
echo ${myname}

printf

语法:

printf  format-string  [arguments...]
format-string: 为格式控制字符串
arguments: 为参数列表。
printf "%-10s %-8s %-4s
" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f
" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f
" 杨过 男 48.6543 
printf "%-10s %-8s %-4.2f
" 郭芙 女 47.9876 

循环

  • for

语法:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

案例:

for loop in 1 2 3 4 5
do
    echo "The value is:"$loop
done
for str in 'This' 'is' 'a' 'string'
do
    echo $str
done
  • while

语法:

while condition
do
    command
done

案例:

int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done
  • 死循环

语法:

while :
do
    command
done

while true
do
    command
done

for (( ; ; ))

更多shell编程可以查看菜鸟教程:http://www.runoob.com/linux/linux-shell.html

coydone的博客
原文地址:https://www.cnblogs.com/coydone/p/13920421.html