参考尚硅谷shell教学
1简介
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。
(1)Linux提供的Shell解析器有
(2)Centos默认的解析器是bash
echo $SHELL
/bin/bash
2入门
1.脚本格式
脚本以#!/bin/bash开头(指定解析器)
2.第一个Shell脚本:helloworld
在helloworld.sh中输入如下内容
#!/bin/bash
echo “helloworld”
(3)脚本的常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限
第二种:采用输入脚本的绝对路径或相对路径执行脚本 ./文件名(必须具有可执行权限+x) chmod 777 helloworld.sh
3.if case for的基础用法
1.if
touch if.sh
**#!/bin/bash
if [ $1 -eq 1 ]
then
echo "banzhang zhen shuai"
elif [ $1 -eq 2 ]
then
echo "cls zhen mei"
fi
**
2.简单for循环
#!/bin/bash
s=0
for((i=1;i<=100;i++))
do
s=$[ $s+$i ]
done
echo $s
#!/bin/bash
for i in "$*"
do
echo "ni xihuan $i"
done
for j in "$@"
do
echo "banzhan xihuan $j"
done
3.case
#!/bin/bash
case $1 in
1) echo "banzhang"
;;
2) echo "cls"
;;
*) echo "renyao"
;;
esac
while
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
s=$[$s+$i]
i=$[$i+1]
done
echo $s
链接:https://pan.baidu.com/s/1kkK_qQ2ha9pSS6j3eS2qDA
提取码:40xg