Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于更新多个文件夹的命令的Linux shell脚本_Linux_Shell_Scripting - Fatal编程技术网

用于更新多个文件夹的命令的Linux shell脚本

用于更新多个文件夹的命令的Linux shell脚本,linux,shell,scripting,Linux,Shell,Scripting,我创建了一个脚本,它只是一个命令列表(cp、mkdir、rm等)。这基本上就是用源文件夹的内容更新一堆文件夹的内容 现在我有大约15个文件夹需要修改,每个文件夹大约有30个命令。因此,当我需要添加文件夹时,我需要再添加30个命令并指定该文件夹 脚本中有没有一种方法可以创建一个文件夹数组来进行更改和循环 我的脚本现在只包含通常在命令行中运行的基本命令,因此没有高级命令。是的,您可以执行以下操作: for x in "folder1" "folder2" "folder3"; do mkdir

我创建了一个脚本,它只是一个命令列表(cp、mkdir、rm等)。这基本上就是用源文件夹的内容更新一堆文件夹的内容

现在我有大约15个文件夹需要修改,每个文件夹大约有30个命令。因此,当我需要添加文件夹时,我需要再添加30个命令并指定该文件夹

脚本中有没有一种方法可以创建一个文件夹数组来进行更改和循环


我的脚本现在只包含通常在命令行中运行的基本命令,因此没有高级命令。

是的,您可以执行以下操作:

for x in "folder1" "folder2" "folder3"; do
  mkdir $x
  cp foobar $x
done
更好的方法是使用数组来保存文件夹名称,例如

arr=("folder1" "folder2" "folder3")

for x in ${arr[*]} do
  mkdir $x
  cp foobar $x
done
如果您有一个模式下的特定名称,您可能可以使用循环自动生成该名称列表。

以下是一种方法:

#!/bin/bash

# This function does all you clever stuff
# $1 contains the first parameter, $2 the second and so on
function my_cmds()
{
    echo $1
}

# You can loop over folders like this
for i in folder1 folder2 folder3
do
    # here we call a function with the string as parameter 
    my_cmds $i
done

# ... or like this
folders[0]="folder0"
folders[1]="folders1"
folders[2]="folders2"

for i in "${folders[@]}"
do
    my_cmds $i
done
初始化整个阵列的一种方便方法是

array=( element1 element2 ... elementN )

注释。

这类似于使用
进行循环的答案,但使用here文档存储文件夹列表。这就像在脚本中嵌入了一个数据文件

while read -r folder <&3; do
    mkdir "$folder"
    # etc
done 3<<EOF
folder1
folder2
folder with space
EOF

read-r文件夹谢谢,这正是我想要的