简体   繁体   English

Bash:循环时将目录名称设置为变量

[英]Bash: set name of directory as a variable while looping

I have a directory containing a big number of sub-directories within.我有一个包含大量子目录的目录。 I need to loop over all subdiretories and save it names (without a path!) as a distinct variable我需要遍历所有子目录并将其名称(没有路径!)保存为一个不同的变量

for d in ${output}/*/
do
   dir_name=${d%*/}
   echo ${dir_name}
done

the problem of the current version that it gives me a full path of the directory instead.当前版本的问题是它给了我目录的完整路径。 Here is the result of echo这是回声的结果

/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig992
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig993
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig994
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig995
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig996
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig997
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig998
/Users/gleb/Desktop/DOcking/clusterizator/sub_folders_to_analyse/7000_CNE_lig999

With the dir_name=${d%*/} , you remove the trailing / only.使用dir_name=${d%*/} ,您只删除尾随/ You will want to remove everything upto the last / as well.您还需要删除所有内容,直到最后一个/ Or try basename , which is perhapse a better option.或者尝试basename ,这也许是更好的选择。

As in:如:

for d in /var/*/ ; do  
    dir_name=${d%/}
    base=$(basename "$d")
    echo "$d $dir_name  ${dir_name##*/} $base"
done

which produces:产生:

/var/adm/ /var/adm  adm adm
/var/cache/ /var/cache  cache cache
/var/db/ /var/db  db db
/var/empty/ /var/empty  empty empty
/var/games/ /var/games  games games
/var/heimdal/ /var/heimdal  heimdal heimdal
/var/kerberos/ /var/kerberos  kerberos kerberos
/var/lib/ /var/lib  lib lib
/var/lock/ /var/lock  lock lock
/var/log/ /var/log  log log
/var/mail/ /var/mail  mail mail
/var/man/ /var/man  man man
/var/named/ /var/named  named named
/var/netatalk/ /var/netatalk  netatalk netatalk
/var/run/ /var/run  run run
/var/slapt-get/ /var/slapt-get  slapt-get slapt-get
/var/spool/ /var/spool  spool spool
/var/state/ /var/state  state state
/var/tmp/ /var/tmp  tmp tmp
/var/www/ /var/www  www www
/var/yp/ /var/yp  yp yp

(on my system). (在我的系统上)。

Can you cd to that parent directory?你可以cd到那个父目录吗?

cd ${output}/
lst=( */ )
for d in "${lst[@]}"; do echo "${d*/}"; done

If that's not an option, then you can strip it each time.如果那不是一个选项,那么您可以每次都将其剥离。

lst=( ${output}/*/ )
for d in "${lst[@]}"; do dir="${d*/}"; echo "${dir##/}"; done

As a hybrid, you can sometimes use a trick of changing directory inside a subshell, as the cd is local to the subshell and "goes away" when it ends, but so do any assignments.作为一个混合体,您有时可以使用在子 shell 中更改目录的技巧,因为cd是子 shell 的本地文件,并且在它结束时“消失”,但任何分配也是如此。

cd /tmp
( cd ${output}/; lst=( */ ); for d in "${lst[@]}"; do echo "${d*/}"; done )
# in /tmp here, lst array does not exist any more...

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

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