shell脚本:and和or



#! /bin/sh

touch file_one
rm -f file_two

if [ -f file_one ] && echo "hello " && [ -f file-two ] && echo "world"
then
	echo "in if"
else
	echo "in else"
fi



and语句和or语句常常一起使用,and语句是当前一个语句为真的时候,才会执行下一个,or语句先一个语句执行不成功时,才会执行下一个。 and示例:


                
Shell脚本是一种为shell编写的脚本程序,可实现自动化任务,提升工作效率。以下为其相关介绍使用方法: ### 编写与执行 在Linux中执行Shell脚本有4种方法,使用前三种方法执行时,会在当前Shell(父Shell)中启动子Shell环境,并在子Shell环境中执行脚本,执行完后关闭子Shell环境,返回父Shell;第四种方法在当前shell中执行[^3]。 ### 传参 可向shell脚本传递参数,以`test.sh`为例: ```bash #!/bin/sh declare -i win_count=0 declare -i lin_count=0 function help() { echo -e "\033[36;1mhelp:\033[0m" echo -e "\033[36;1m --win(-W) : Windows Number of Windows VMS(eg: -W 2 or --win 2)\033[0m" echo -e "\033[36;1m --Lin(-L) : Linux Number of Linux VMS(eg: -L 2 or --lin 2)\033[0m" echo -e "\033[36;1m --help(-h): display this help and exit \033[0m" } function isLegal() { machine_num=$1 if [ $machine_num -eq 0 ]; then echo "[-] Parameters of illegal !!" help exit 1 fi } if [[ $1 = "--help" ]] || [[ $1 = "-h" ]]; then help exit 1 fi if [ ! -n "$1" ]; then echo "[-] Parameters of illegal !" help exit 1 fi while [ -n "$1" ] do case "$1" in --win) win_count=$2 shift ;; -W) win_count=$2 shift ;; --lin) lin_count=$2 shift ;; -L) lin_count=$2 shift ;; *) echo "[-] Parameters of illegal !!!" help exit 1 ;; esac shift done isLegal $win_count isLegal $lin_count echo $win_count --- $lin_count ``` 该脚本展示了如何接收处理传入的参数,根据不同的参数选项设置相应的变量值,并进行合法性检查[^4]。 ### for循环 在Shell脚本中不止有一种for循环使用方法,以下示例展示了一种常用的for循环使用方法: ```bash #!/bin/bash for (( i=1; i<6; i++)); do echo "i is: ${i}" done ``` 此示例中,循环变量`i`从1开始,每次循环递增1,直到`i`小于6,每次循环都会输出`i`的值[^1]。 ### 调用其他脚本 有时会在一个shell脚本(如`test_call_other_shell.sh`)中调用另外一个shell脚本(如`parameter_usage.sh`),可行的方法有: 1. 通过`source`:运行在相同的进程,调用后,被调用脚本中的变量函数在调用脚本中可直接使用。 2. 通过`/bin/bash`:运行在不同的进程。 3. 通过`sh`:运行在不同的进程。 4. 通过`.`:运行在相同的进程,调用后,被调用脚本中的变量函数在调用脚本中可直接使用 [^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值