侧边栏壁纸
博主头像
coydone博主等级

记录学习,分享生活的个人站点

  • 累计撰写 306 篇文章
  • 累计创建 51 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Shell基本使用

coydone
2022-10-05 / 0 评论 / 0 点赞 / 324 阅读 / 1,079 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-05-03,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

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\n" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 
printf "%-10s %-8s %-4.2f\n" 郭芙 女 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。

0

评论区