首页 > 其他分享 >[Bash] Controls flow

[Bash] Controls flow

时间:2024-05-19 16:18:23浏览次数:28  
标签:case commands color flow number Controls echo file Bash

If

The if statement is used to execute commands based on a condition.

if [ condition ]; then
    commands
fi

Example: vim example.sh

#!/bin/zsh

echo "Enter a number: "
read number

if [ $number -gt 0 ]; then
  echo "The number is positive."
elif [ $number -lt 0 ]; then
  echo "The number is negative."
else
  echo "The number is zero."
fi

chmod +x example.sh

Case

The case statement is used to execute commands based on multiple conditions.

case value in
  pattern1)
    commands1
    ;;
  pattern2)
    commands2
    ;;
  *)
    default_commands
    ;;
esac

Example

#!/bin/zsh

echo "Enter a letter: "
read letter

case $letter in
  [a-z])
    echo "You entered a lowercase letter."
    ;;
  [A-Z])
    echo "You entered an uppercase letter."
    ;;
  *)
    echo "You entered a non-alphabet character."
    ;;
esac

For

The for loop is used to iterate over a list of items.

for item in list; do
  commands
done

Example:

#!/bin/zsh

for i in {1..5}; do
  echo "Number: $i"
done

Pratices:

# Write an if statement to check if a file named myfile.txt exists and print a message accordingly.

FILENAME="myfile.txt"
if [ -f $FILENAME ]; then
  echo "$FILENAME exists"
else
  echo "No such file"
fi


# Write a case statement to check if a variable color is "red", "green", or "blue" and print a message for each color.

echo "Enter a color: "
read color

case $color in
  red)
    echo "red'
    ;;
  blue)
    echo "blue"
    ;;
  green)
    echo "green"
    ;;
  *)
    echo "not find a match color"
    ;;

# Write a for loop to print the names of all files in the current directory.

for file in *; do
  if [ -f "$file" ]; then
    echo "File: $file"
    # size=$(stat -c%s "$file")
    size=$(stat -f%z "$file")
    echo "File: $file, Size: $size bytes"
  fi
done

Output:

File: if.sh, Size: 105 bytes
File: list_regular_files.sh
File: list_regular_files.sh, Size: 162 bytes

You can check the file size by using ls -lh as well

for file in *; do: This loop iterates over all items in the current directory.
if [ -f "$file" ]; then: Checks if the item is a regular file.

标签:case,commands,color,flow,number,Controls,echo,file,Bash
From: https://www.cnblogs.com/Answer1215/p/18200438

相关文章

  • [Bash] env
    Commands:export:Setenvironmentvariables.env:Viewenvironmentvariables.echo:Displayenvironmentvariables.CommonEnvironmentVariables:PATH:Directorieswheretheshelllooksforexecutablefiles.HOME:User'shomedirectory.USER:Current......
  • [Bash] chmod and chown
    UnderstandingFilePermissions:FilepermissionsinUnix-likesystemsdeterminewhocanread,write,orexecuteafile.Theyarerepresentedasacombinationofthreegroups:user(u),group(g),andothers(o).-rwxr-xr--rstandsforreadpermission.w......
  • from tensorflow.examples.tutorials.mnist import input_data时input_data报错
    参考链接——https://blog.csdn.net/weixin_42390287/article/details/115635732 报错 ——解决方法去https://github.com/tensorflow/tensorflow下载压缩包 解压,找到input_data.py后复制到一个新建的空文件夹里将这个文件夹放到python项目中,代码中对input_......
  • Flower 监控celery任务
    Flower监控celery任务如果不想通django的管理界面监控任务的执行,还可以通过Flower插件来进行任务的监控。Flower的界面更加丰富,可以监控的信息更全Flower是一个用于监控和管理Celery集群的开源Web应用程序。它提供有关Celeryworkers和tasks状态的实时信息功能【1】......
  • [Bash] sed command
    Thesedcommandisastreameditorusedforfilteringandtransformingtext.sed'command'fileCreateandviewtheinitialcontentofsample.txt:echo-e"HelloWorld\nThisisasamplefile\nBashscriptingispowerful\nLearninggrep,aw......
  • iFlow实验笔记
    一、架构设计与仿真1架构设计因为我参加了第六期一生一芯,因此使用自己的设计。芯片架构图如下,采用RISCV32IE指令集,并包括ZiCSR指令拓展。由于一生一芯的宗旨是先完成后完美,而我正在进行SoC计算机系统的搭建,通过AXI4总线Xbar接入SoC部分。因此CPU的微架构比较简单,还未引入流水......
  • 通过BASH脚本实现DNS优选
    02***/root/mysqlbeifen.sh*/10****/root/dns_update.sh#!/bin/bashLOG_DIR="/var/log/dns_script"HOST_FILE="/etc/hosts_NC"DOMAIN="sso.ccnhub.com"DNS_SERVER="114.114.114.114"#ReplacewithyourDN......
  • bash脚本监控服务器SSH登录,每30分钟运行一次,发现登录发送到企业微信群
    //开始循环检测//loopCheck();//在每分钟的第30秒执行目标函数cron.schedule('358***',()=>{console.log('目标函数在8:35执行!');loopCheck_info();//在这里调用你想要定时执行的函数});cron.schedule('*/309-20***',()=>{con......
  • Summer '24:不容错过的Salesforce Flow 10大新功能!
    Flow是整个Salesforce平台自动化的未来。自从WorkflowRules和ProcessBuilders被淘汰,Salesforce将重点放在了Flow上,一直在将大量资源用于开发Flow创新,本次Summer'24中Flow也有不少亮眼的更新!01FlowCreationWizard和Flow类型创建Flow与以前有所不同。你看到的第一步只是让......
  • SciTech-BigDataAIML-Tensorflow-模型的训练与评估: tf.keras.losses + tf.keras.optim
    模型的训练:tf.keras.losses和tf.keras.optimizer定义一些模型超参数:num_epochs=5batch_size=50learning_rate=0.001实例化模型和数据读取类,并实例化一个tf.keras.optimizer的优化器(这里使用常用的Adam优化器):model=MLP()data_loader=MNISTLoader()optimiz......