LINUX:列出所有目录,推入一个 bash 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4720454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINUX: List all directories, push into a bash array
提问by Snow_Mac
Here's the end result I am trying to:
这是我想要的最终结果:
I have over 15 users of an cloned instance of my application, sometimes I need to update files (they pretty much all stay the same--everything is dynamic. This is for updates/new features). I wrote a pretty simple bash script that I had to manually put each user from /home/ into the array. But I need this to scale.
我的应用程序的克隆实例有超过 15 个用户,有时我需要更新文件(它们几乎都保持不变——一切都是动态的。这是用于更新/新功能)。我编写了一个非常简单的 bash 脚本,我必须手动将 /home/ 中的每个用户放入数组中。但我需要这个来扩展。
How can I take a directory listing (something like a LS command) feed ONLY DIRECTORY names into then a bash array. Likely i'll want this command in the bash file though, because I'll want it to grab all users in the /home/ directory, push into the array (eg: webUsers( adam john Hyman )
我如何才能将目录列表(类似于 LS 命令)仅将 DIRECTORY 名称输入到 bash 数组中。不过,我可能希望在 bash 文件中使用此命令,因为我希望它获取 /home/ 目录中的所有用户,并将其推送到数组中(例如:webUsers(adam john Hyman)
Here's a snapshot of what my current script looks like (non-dynamic user listing)
这是我当前脚本的快照(非动态用户列表)
webUsers( adam john Hyman )
for i in "${webUsers[@]}"
do
cp /home/mainSource/public_html/templates/_top.tpl /home/$i/public_html/templates
done
How do I achieve this?
我如何实现这一目标?
回答by Paused until further notice.
Do this:
做这个:
webUsers=(/home/*/)
and the contents will look like:
内容将如下所示:
$ declare -p webUsers
declare -a webUsers='([0]="/home/adam/" [1]="/home/Hyman/" [2]="/home/john")'
$ echo ${webUsers[1]}
/home/Hyman/
Or, if you don't want the parent directory:
或者,如果您不想要父目录:
pushd /home
webUsers=(*/)
popd
and you'll get:
你会得到:
$ declare -p webUsers
declare -a webUsers='([0]="adam/" [1]="Hyman/" [2]="john")'
$ echo ${webUsers[1]}
Hyman/
回答by SiegeX
The following script will loop over all users with directories in /home. It will also unconditionally try to create the /public_html/templatesdirectory. If it doesn't yet exist, it will get created. If it doesexist, this command does essentially nothing.
以下脚本将遍历所有目录在/home. 它也会无条件地尝试创建/public_html/templates目录。如果它还不存在,它将被创建。如果它确实存在,这个命令基本上什么都不做。
#!/bin/bash
cd /home
userarr=( */ );
for user in "${userarr[@]%*/}"; do
mkdir -p "/home/${user}/public_html/templates"
cp "/home/mainSource/public_html/templates/_top.tpl /home/${user}/public_html/templates"
done
回答by regality
It may be easier to make a link to the source directory, and then you can just update it in one place.
建立指向源目录的链接可能更容易,然后您只需在一个地方更新它。
Just set up each users directory so that the common files are all pulled from a directory called common_files (or whatever you like), and then run this command in each home directory:
只需设置每个用户目录,以便从名为 common_files(或任何您喜欢的目录)的目录中提取所有公共文件,然后在每个主目录中运行此命令:
ln -s /location/of/files/they/need common_files
update /location/of/files/they/need and it automatically propagates.
更新 /location/of/files/they/need 并自动传播。
回答by jan
With bash you can actually make this pretty short and simple. To list the current directory and store it into an array:
使用 bash,您实际上可以使这变得非常简短和简单。列出当前目录并将其存储到数组中:
ls . | readarray i
or
或者
ls . | bash -c 'readarray i'
ls . | bash -c 'readarray i'
To use the data:
要使用数据:
ls . | bash -c 'readarray i && for j in ${i[*]}; do <-command->; done'
ls . | bash -c 'readarray i && for j in ${i[*]}; do <-command->; done'

