shell linux 常用脚本
时间: 2023-09-30 14:03:56 浏览: 48
1. Hello World
echo "Hello World"
2. 判断文件是否存在
if [ -e file_name ]; then
echo "file exists"
else
echo "file does not exist"
fi
3. 循环
for i in {1..10}
do
echo $i
done
4. 输出当前时间
date
5. 复制文件
cp file1 file2
6. 删除文件
rm file_name
7. 查看文件内容
cat file_name
8. 判断字符串是否相等
if [ "$str1" = "$str2" ]; then
echo "strings are equal"
else
echo "strings are not equal"
fi
9. 判断数字大小
if [ $num1 -gt $num2 ]; then
echo "num1 is greater than num2"
else
echo "num1 is less than or equal to num2"
fi
10. 定义变量
var_name="value"
11. 判断目录是否存在
if [ -d dir_name ]; then
echo "directory exists"
else
echo "directory does not exist"
fi
12. 输出环境变量
echo $PATH
13. 判断文件是否可读
if [ -r file_name ]; then
echo "file is readable"
else
echo "file is not readable"
fi
14. 判断文件是否可写
if [ -w file_name ]; then
echo "file is writable"
else
echo "file is not writable"
fi
15. 判断文件是否可执行
if [ -x file_name ]; then
echo "file is executable"
else
echo "file is not executable"
fi
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)