arrays – bash列出文件夹中的所有子目录,将它们写入数组以在菜
发布时间:2020-12-16 01:48:40 所属栏目:安全 来源:网络整理
导读:我正在写一个bash脚本,我在某个目录中有多个子文件夹,我想列出子文件夹的名称,并将结果读入一个数组,从结果中省略一个名为’cmmdm’的特定文件夹.一旦我将名称读入数组,我想生成一个菜单,每个子菜单名称作为一个选项,然后我将根据用户的选择在给定的子文件夹
我正在写一个bash脚本,我在某个目录中有多个子文件夹,我想列出子文件夹的名称,并将结果读入一个数组,从结果中省略一个名为’cmmdm’的特定文件夹.一旦我将名称读入数组,我想生成一个菜单,每个子菜单名称作为一个选项,然后我将根据用户的选择在给定的子文件夹上执行一个功能.
编辑: #!/bin/bash # - create array declare -a CDARRAY # - set 0 to exit in prep for menu CDARRAY[0]=exit # - create a counter to use in while loop count=1 # - while loop to itterate through folder and add each folder except cmmdm into array ls -d /home/nginx/domains/* | { while read CMMDOMAIN ; do if [ $CMMDOMAIN != "/home/nginx/domains/cmmdm" ] then $CDARRAY[$count]=$CMMDOMAIN echo $CDARRAY[$count] count=$[count + 1] fi done } 这确实通过文件夹并忽略’cmmdm’但是我的代码将变量CMMDOMAIN添加到数组是错误的.我以前从未在bash中编写过脚本,所以我想我可能已经弄错了语法或者缺少一些大括号或者其他东西 解决方法
你的代码有很多问题,在这里讨论太多了(没有冒犯).
这是一个完整的示例,可以显示您想要的菜单,并进行一些常见的检查: #!/bin/bash shopt -s extglob nullglob basedir=/home/nginx/domains # You may omit the following subdirectories # the syntax is that of extended globs,e.g.,# omitdir="cmmdm|not_this_+([[:digit:]])|keep_away*" # If you don't want to omit any subdirectories,leave empty: omitdir= omitdir=cmmdm # Create array if [[ -z $omitdir ]]; then cdarray=( "$basedir"/*/ ) else cdarray=( "$basedir"/!($omitdir)/ ) fi # remove leading basedir: cdarray=( "${cdarray[@]#"$basedir/"}" ) # remove trailing backslash and insert Exit choice cdarray=( Exit "${cdarray[@]%/}" ) # At this point you have a nice array cdarray,indexed from 0 (for Exit) # that contains Exit and all the subdirectories of $basedir # (except the omitted ones) # You should check that you have at least one directory in there: if ((${#cdarray[@]}<=1)); then printf 'No subdirectories found. Exiting.n' exit 0 fi # Display the menu: printf 'Please choose from the following. Enter 0 to exit.n' for i in "${!cdarray[@]}"; do printf ' %d %sn' "$i" "${cdarray[i]}" done printf 'n' # Now wait for user input while true; do read -e -r -p 'Your choice: ' choice # Check that user's choice is a valid number if [[ $choice = +([[:digit:]]) ]]; then # Force the number to be interpreted in radix 10 ((choice=10#$choice)) # Check that choice is a valid choice ((choice<${#cdarray[@]})) && break fi printf 'Invalid choice,please start again.n' done # At this point,you're sure the variable choice contains # a valid choice. if ((choice==0)); then printf 'Good bye.n' exit 0 fi # Now you can work with subdirectory: printf "You chose subdirectory `%s'. It's a good choice.n" "${cdarray[choice]}" 评论应该清楚地解释发生了什么.用于构建数组的技术,这是您的问题的目的,是extended globs.例如: shopt -s extglob nullglob cdarray=( /home/nginx/domains/!(cmmdm)/ ) 将填充cdarray与/ home / nginx / domains /的所有子目录不匹配cmmdm(完全匹配).要使所有不以a或b结尾的子目录: shopt -s extglob nullglob cdarray=( /home/nginx/domains/!(*[ab])/ ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Angular2 ng-bootstrap模态组件
- ( 一)bootstrap学习——初识bootstrap
- Bootstrap Hello World
- 【WebService学习过程记录(二)】Java6+Servlet+tomcat发布H
- scala – 提升Shtml.link
- WebService异常Unexpected wrapper element ... found. Exp
- 如何设置-e选项时如何避免bash脚本失败?
- 这个其他版本的bash fork炸弹是如何工作的?
- Codeforces 999D Equalize the Remainders 【数据结构】【贪
- 使用shell脚本调用Elixir方法
推荐文章
站长推荐
热点阅读