linux shell脚本基础语法
时间: 2023-11-03 20:59:43 浏览: 152
Linux shell脚本基础学习详细介绍(完整版)第1/2页
Shell是一个用C语言编写的程序,它是一种命令行解释器,也是用户使用Linux内核的桥梁。Shell脚本程序通常以#!/bin/bash开头,并且需要有可执行权限才能运行。下面是一些基础语法的示例:
1. 简单输出:
```shell
#!/bin/bash
echo "Hello World!"
```
2. 变量的声明和使用:
```shell
#!/bin/bash
NAME="zhang san"
DT=`date '+%F'`
DT2=$(date)
echo "My name is $NAME"
echo "Today is $DT"
echo "Current date and time: $DT2"
```
3. 条件判断:
```shell
#!/bin/bash
SYSTEM=`uname -s`
case $SYSTEM in
Linux)
echo "My system is Linux"
echo "Do Linux stuff here..."
;;
FreeBSD)
echo "My system is FreeBSD"
echo "Do FreeBSD stuff here..."
;;
*)
echo "Unknown system: $SYSTEM"
echo "I don't know what to do..."
;;
esac
```
4. 循环:
```shell
#!/bin/bash
for i in {1..5}
do
echo "Number: $i"
done
```
5. 格式化输出:
```shell
#!/bin/bash
printf "Hello, %s!\n" "World"
printf "Number: %d\n" 42
```
以上是一些基础的Shell脚本语法,希望对你有帮助。
阅读全文