常用shell脚本

  1. HelloWorld脚本:
#!/bin/bash
echo "Hello World!"
  1. 读取用户输入并输出脚本:
#!/bin/bash
echo "Please enter your name:"
read name
echo "Hello $name!"
  1. 循环脚本:
#!/bin/bash
for i in {1..10}
do
   echo "Count: $i"
done
  1. 判断是否存在指定文件脚本:
#!/bin/bash
if [ -f "/path/to/file" ]
then
    echo "File exists."
else
    echo "File does not exist."
fi
  1. 判断两个数大小关系脚本:
#!/bin/bash
echo "Enter two numbers:"
read num1
read num2
if [ $num1 -gt $num2 ]
then
    echo "$num1 is greater than $num2"
elif [ $num1 -eq $num2 ]
then
    echo "$num1 is equal to $num2"
else
    echo "$num1 is less than $num2"
fi
  1. 计算两个数的和脚本:
#!/bin/bash
echo "Enter two numbers:"
read num1
read num2
sum=$(($num1 + $num2))
echo "The sum of $num1 and $num2 is: $sum"
  1. 判断文件夹是否存在脚本:
#!/bin/bash
if [ -d "/path/to/folder" ]
then
    echo "Folder exists."
else
    echo "Folder does not exist."
fi
  1. 删除指定文件脚本:
#!/bin/bash
rm /path/to/file
echo "File deleted."
  1. 复制指定文件脚本:
#!/bin/bash
cp /path/to/source /path/to/destination
echo "File copied."
  1. 输出当前系统时间脚本:
#!/bin/bash
now=$(date +"%T")
echo "Current time: $now"
常用shell脚本

原文地址: https://www.cveoy.top/t/topic/dveD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录