简体   繁体   English

LINUX:列出所有目录,推入bash数组

[英]LINUX: List all directories, push into a bash array

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). 我有超过15个用户的克隆应用程序的实例,有时我需要更新文件(它们几乎保持不变 - 一切都是动态的。这是更新/新功能)。 I wrote a pretty simple bash script that I had to manually put each user from /home/ into the array. 我写了一个非常简单的bash脚本,我必须手动将每个用户从/ home /放入数组中。 But I need this to scale. 但我需要这个扩展。

How can I take a directory listing (something like a LS command) feed ONLY DIRECTORY names into then a bash array. 如何获取目录列表(类似于LS命令)仅将DIRECTORY名称提供给bash数组。 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 jack ) 可能我会在bash文件中想要这个命令,因为我希望它抓住/ home /目录中的所有用户,推入阵列(例如:webUsers(adam john jack)

Here's a snapshot of what my current script looks like (non-dynamic user listing) 这是我当前脚本的样子快照(非动态用户列表)

webUsers( adam john jack )

for i in "${webUsers[@]}"
do 
 cp /home/mainSource/public_html/templates/_top.tpl /home/$i/public_html/templates
done 

How do I achieve this? 我该如何实现这一目标?

Do this: 做这个:

webUsers=(/home/*/)

and the contents will look like: 内容如下:

$ declare -p webUsers
declare -a webUsers='([0]="/home/adam/" [1]="/home/jack/" [2]="/home/john")'
$ echo ${webUsers[1]}
/home/jack/

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]="jack/" [2]="john")'
$ echo ${webUsers[1]}
jack/

The following script will loop over all users with directories in /home . 以下脚本将遍历包含/home目录的所有用户。 It will also unconditionally try to create the /public_html/templates directory. 它还将无条件地尝试创建/public_html/templates目录。 If it doesn't yet exist, it will get created. 如果它还不存在,它将被创建。 If it does exist, this command does essentially nothing. 如果确实存在,则此命令基本上不执行任何操作。

#!/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

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. 更新/位置/ / files /他们/需要它自动传播。

With bash you can actually make this pretty short and simple. 有了bash,你实际上可以做到这么短而简单。 To list the current directory and store it into an array: 列出当前目录并将其存储到数组中:

ls . | readarray i

or 要么

ls . | bash -c 'readarray i'

To use the data: 要使用数据:

ls . | bash -c 'readarray i && for j in ${i[*]}; do <-command->; done'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM