首页 > 其他分享 >[Bash] for loop

[Bash] for loop

时间:2024-06-11 18:44:08浏览次数:8  
标签:bin do done loop file backup Example Bash

The basic syntax of a for loop in Bash is:

for variable in list
do
  commands
done

Examples

Example 1: Iterating Over a List of Words

#!/bin/zsh

for word in apple banana cherry
do
  echo "The word is: $word"
done 

Example 2: Iterating Over a Range of Numbers

#!/bin/zsh

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

Example 3: Iterating Over Files in a Directory

#!/bin/zsh

for file in /path/to/directory/*
do
  echo "Processing file: $file"
done 

Example 4: Using Command Substitution

#!/bin/zsh

for user in $(cut -d: -f1 /etc/passwd)
do
  echo "User: $user"
done

for user in $(cut -d: -f1 /etc/passwd): The loop iterates over all usernames in the /etc/passwd file.

Example 5: C-style for Loop

#!/bin/zsh

for ((i=1; i<=5; i++))
do
  echo "Number: $i"
done

Practical Example: Creating Backup Files

Create the script file backup.sh:

nano backup.sh

Add the following content:

#!/bin/zsh

source_dir="/path/to/source_directory"
backup_dir="/path/to/backup_directory"

# create the backup directory if it doesn't exist
mkdir -p "$backup_dir"

foir file in "$source_dir"
do
  # Extract the filename from the path
  filename=$(basename "$file")

  # Create a backup copy in the backup directory
  cp "$file" "$backup_dir/$filename.bak"

  echo "Created backup of $file as $fileanme.bak"
done

标签:bin,do,done,loop,file,backup,Example,Bash
From: https://www.cnblogs.com/Answer1215/p/18242559

相关文章

  • shell和bash脚本命令学习
    Shell指的是任何提供命令行界面的程序,它提供了用户与操作系统之间的交互界面。它允许用户输入命令并执行它们,同时提供脚本编写功能,使得自动化任务成为可能。Shell有多种类型,比如BourneShell(sh)、CShell(csh)、KornShell(ksh)等。Bash是Shell的一种,全称为BourneAgainShell,是Bourne......
  • 面试专区|【40道Bash Shell高频题整理(附答案背诵版)】
    1.简述如何调试Shell脚本?调试Shell脚本是一个帮助开发者识别和修正脚本中错误的过程。Bash提供了多种方式来调试脚本,其中包括:使用-x选项:通过在运行脚本时使用-x选项,Bash会在执行每一行命令之前打印该命令。这有助于查看脚本的执行流程和变量的值变化。例如,如......
  • route_localnet decides whether a loopback-hosting server can server requests out
    BackgroundWhenIwasfollowingtheRAGexamplepromptflow-resource-hubtotracemyapplicationthroughapromtflowserverhostedontheloopbackinterface,asthelocalenvisavirtualmachineonAzure,andafterIaddNSGruletoallowtherequeststo......
  • [Bash] quotes
    SinglequotesIfyouwanttousecharacterslike<or>intheargumentstoaprogram,youwillneedtousequotessothattheshelldoesn'ttrytointerpretthem.Forexample,toechothestring<b>wow</b>wecanusesinglequotes:......
  • [Bash] Environment variables
    Environmentvariablesaredefinedbytheshellandshellscripts.Tolistthecurrentenvironmentvariables,typeexport:~$exportdeclare-xDISPLAY=":0"declare-xHOME="/home/substack"declare-xHUSHLOGIN="FALSE"declar......
  • 在Linux中,什么是 BASH?
    在Linux中,BASH(BourneAgainSHell)是一个广泛使用的shell,也是大多数Linux发行版的默认shell。BASH是Bourneshell(sh)的一个替代品,由BrianFox和ChetRamey编写,以改进sh的功能和易用性。以下是关于BASH的详细解释:1.定义BASH是一个命令行解释器(也称为shell),它允许用户与操作系统进行......
  • [Bash] Backticks, xargs and Arithmetic
    BackticksUsingtoexeccmdandreturntheoutputasstring$echo`date`FriJun715:40:11EEST2024Thesameeffectyoucanachievebyusing$echo$(date)FriJun715:40:11EEST2024Examples$echo`date+%F`2024-06-07$echosomelogdata>......
  • 如何用在bash中截取部分系统参数?
    背景在编程过程中遇到一个问题。在一个bash脚本中,我需要用lame重新生成音频文件的压缩版本。原有的参数为三个:种子值、源txt文件和目标wav文件。我希望的是能够顺便自动生成压缩的mp3版本以及添加了背景音乐的版本,但是我又不希望添加新的输入参数。这样,对于原本的系统参数进行切......
  • 在Linux中,BASH 和 DOS之间的区别是什么?
    BASH(BourneAgainSHell)和DOS(DiskOperatingSystem)之间存在显著的区别,这些差异不仅体现在它们的设计哲学、功能特性上,也反映在它们所服务的操作系统环境及其用途上。以下是一些主要的区别:性质和定位:BASH:是一种命令行解释器(shell),它是用户与Linux或其他类UNIX操作系统交互......
  • C# NewtonJson Self referencing loop detected for property 'Parent' with type
    privatevoidImage_MouseLeftButtonDown(objectsender,MouseButtonEventArgse){stringimgJson1=JsonConvert.SerializeObject(img1);System.IO.File.AppendAllText($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}_img.json",imgJso......