Linux Shell 编程

#!/bin/bash

# 使用shell来执行
sh hello.sh
# 使用bash来执行
bash hello.sh
# 使用.来执行
. ./hello.sh
# 使用source来执行
source hello.sh
# 还可以让脚本本有者该执行权限,允许该用户执行该脚本
chmod u+rx hello.sh
# 执行命令,这身就具有可执行权限,通过chmod命令可以修改:
# 赋予脚本的所将使用脚本第一行指定的shell来执行,如果指定shell不存在,将使用系统默认shell来执行
 ./hello.sh

# 初始化一个变量
LOG_DIR=/var/log
cd $LOG_DIR
cat /dev/null > wtmp
echo "Logs cleaned up."
exit


# 输入参数
:<<!
read -p "plese input par:" a b
#输出参数
echo $a,$b
#echo ${a},${b}
echo "参数个数为:$#"
echo "脚本名字:$0"
echo "第一个参数:$1"
echo "第二个参数:$2"
!

:<<!
echo "Is it morning? please answer yes or no."
read timeofday

if [ "$timeofday" = "yes" ]
then
    echo "Good morning."
elif [ "$timeofday" = "no" ]
then
    echo "Good afternoon."
else
    echo "Sorry, $timeofday not recognized. Enter yes or no."
    exit 1
fi
!

:<<!
for foo in a b c d
do
    echo $foo
done
!

n=`cat file.txt | tr -s ' ' '\n' | wc -l`
echo $n

my_array=(A B "C" D)
echo ${my_array[0]}
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo "数组的元素为: ${my_array[*]}"
echo "数组的元素为: ${my_array[@]}"
echo "数组元素个数为: ${#my_array[*]}"
echo "数组元素个数为: ${#my_array[@]}"

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

int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

while true
do
    command
done

echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
    1)  echo '你选择了 1'
    ;;
    2)  echo '你选择了 2'
    ;;
    3)  echo '你选择了 3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac

while :
do
    echo -n "输入 1 到 5 之间的数字:"
    read aNum
    case $aNum in
        1|2|3|4|5) echo "你输入的数字为 $aNum!"
        ;;
        *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
            break
        ;;
    esac
done

while :
do
    echo -n "输入 1 到 5 之间的数字: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "你输入的数字为 $aNum!"
        ;;
        *) echo "你输入的数字不是 1 到 5 之间的!"
            continue
            echo "游戏结束"
        ;;
    esac
done

exit 0

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容