如何将子目录列表存储到数组中并通过Bash中的索引访问它们?

时间:2017-06-25 05:49:38

标签: arrays bash

假设我们有一个目录名称" A"有4个子目录(aa,bb,cc,dd),一些子目录也有子目录,所以假设如下的原理图:

  A
      aa
           aaa
      bb
           bbb
           bbbb
      cc
      dd

我尝试在数组中列出子目录(aa,bb,cc,dd),然后在我的脚本中使用它们的数组编号。 我使用下面的脚本将dd复制到父目录:

while IFS= read -d '' file; do
    A+=( "$file" )
done < <(find . -type d -print0 | LC_ALL=C sort -z)

cp -r  `pwd`/${A[4]}"  `pwd`/..

但问题是脚本会生成所有子目录的数组[aa aaa bb bbb bbbb cc dd] 所以${a[4]} = bbb而非dd

知道怎么解决吗?

1 个答案:

答案 0 :(得分:2)

您可以使用 article, aside, audio, canvas, command, datalist, details, embed, figcaption, figure, footer, header, hgroup, keygen, meter, nav, output, progress, section, source, video { display:block; } mark, rp, rt, ruby, summary, time { display:inline; } body { background:#fff; font-family:Arial, Helvetica, sans-serif; font-size:100%; line-height:1em; color:#454545; background-image:url(../images/tail-middle.jpg) repeat-y ; /*background-image: no-repeat;*/ } .dropdown { float: left; overflow: visible; } .dropdown .dropbtn { font-size: 16px; border: none; outline: none; padding: 14px 16px; background-color: inherit; } .dropdown:hover .dropbtn { background-color: #white; } .dropdown-content { display: none; position: absolute; background-color: #grey; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { float: none; color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #ddd; } .dropdown:hover .dropdown-content { display: block; } html { min-width:940px; } html, body { height:100%; } .container { margin:0 auto; width:940px; overflow:visible; font-size:.875em; padding-bottom:176px; } header { background:url(../images/header-bg.jpg) no-repeat center top; min-width:940px; } header .container { height:194px; position:relative; padding:0; } section#content { float:left; width:691px; } aside { width:219px; float:left; margin-right:30px; padding:5px 0 0 0; } .fleft { float:left; } .fright { float:right; } .clear { clear:both; } .col-1, .col-2, .col-3 { float:left; } .alignright { text-align:right; } .aligncenter { text-align:center; } .wrapper { width:100%; overflow:hidden; } .wrap { height:auto !important; height:100%; min-height:100%; background:url(../images/tail-middle.jpg) repeat-y center 194px; min-width:940px; } input, select, textarea { font-family:Arial, Helvetica, sans-serif; font-size:1em; vertical-align:middle; font-weight:normal; } fieldset { border:0; } .categories { padding-bottom:37px; } .categories li { font-weight:bold; font-size:.857em; padding-bottom:8px; margin-bottom:6px; background:url(../images/divider1.gif) repeat-x left bottom; } .categories li.last { background:none; padding:0; margin:0; } .categories li span { display:block; padding-left:15px; height:1%; background:url(../images/marker.gif) no-repeat left 5px; } .categories li a { text-decoration:none; color:#008cc4; } .categories li a:hover { color:#ff7b01; } .news { margin-top:-25px; } .news li { padding-top:25px; line-height:1.429em; } .news li strong { font-size:.857em; color:#454545; display:block; padding-bottom:3px; } .news li a { color:#008cc4; text-decoration:none; font-weight:bold; } .articles { margin-top:-18px; } .articles li { width:100%; overflow:hidden; vertical-align:top; line-height:1.429em; padding-top:18px; } .articles li img { float:left; margin-right:22px; } .sitemap { padding-bottom:15px; } .sitemap li { padding:0 0 5px 16px; background:url(../images/marker1.gif) no-repeat left 5px; } .sitemap li ul { padding-top:5px; margin-bottom:-5px; } .list { width:100%; overflow:hidden; padding-bottom:40px; } .list li { float:left; width:175px; margin-right:53px; } .list li.last { margin:0; } .list li span { display:block; text-align:center; } .list li h4 { padding-top:20px; } .list li p { margin:0; } .img-box { width:100%; overflow:hidden; padding-bottom:20px; line-height:1.429em; } .img-box img { float:left; margin:0 20px 0 0; } .extra-wrap { overflow:hidden; } p { margin-bottom:16px; line-height:1.429em; } .p0 { margin:0; } .address { width:100%; overflow:hidden; padding-bottom:40px; } .address address { font-style:normal; line-height:1.429em; float:left; width:248px; } .address address strong { color:#008cc4; width:94px !important; width:91px; float:left; } .address .extra-wrap { float:left; width:385px; } .address p { margin:0; } #banner { width:691px; height:299px; background:url(../images/banner-bg.jpg) no-repeat left top; } #banner h2 { color:#fff; font-size:42px; text-transform:none; line-height:1.2em; padding:75px 0 0 55px; margin:0; } #banner h2 span { display:block; margin-top:-12px; color:#fff; } #banner h2 span span { font-size:30px; margin-top:-8px; } a { color:#ff7b01; outline:none; } a:hover { text-decoration:none; } h1 { text-indent:-9999px; } h1 a { width:473px; height:63px; position:absolute; left:0; top:4px; background:url(../images/logo.jpg) no-repeat left top; } h2 { font-size:24px; line-height:1.2em; text-transform:uppercase; margin-bottom:20px; } h2 span { color:#8d8d8d; } h3 { font-size:20px; line-height:1.2em; text-transform:uppercase; margin-bottom:15px; } h4 { font-size:1em; color:#008cc4; margin-bottom:5px; } h4 a { color:#008cc4; text-decoration:none; } h4 a:hover { color:#ff7b01; } .txt1 { color:#008cc4; font-weight:bold; } .link { position:absolute; right:0; top:159px; } header .nav-bg { background:no-repeat center top; } header nav { position:absolute; left:0; top:100px; } header nav ul li { float:left; font-size:16px; line-height:1.2em; text-transform:uppercase; padding-right:1px; background:url(../images/divider.gif) repeat-y right top; } header nav ul li.last { background:none; padding:0; } header nav ul li a { color:#454545; text-decoration:none; display:block; float:left; text-align:center; padding:18px 0 20px 0; background-repeat:no-repeat; background-position:left top; } header nav ul li.current a, header nav ul li a:hover { color:#008cc4; } header nav ul li a.m1 { width:95px; } header nav ul li.current a.m1 { background-image:url(../images/m1-act.jpg); } header nav ul li a.m2 { width:80px; } header nav ul li.current a.m2 { background-image:url(../images/m2-act.jpg); } header nav ul li a.m3 { width:85px; } header nav ul li.current a.m3 { background-image:url(../images/m3-act.jpg); } header nav ul li a.m4 { width:60px; } header nav ul li.current a.m4 { background-image:url(../images/m4-act.jpg); } header nav ul li.last { background:none; padding:0; } header nav ul li a.m5 { width:100px; } header nav ul li.current a.m5 { background-image:url(../images/m5-act.jpg); } header nav ul li a.m6 { width:100px; } header nav ul li.current a.m6 { background-image:url(../images/m5-act.jpg); } header nav ul li a.m7 { width:75px; } header nav ul li.current a.m7 { background-image:url(../images/m5-act.jpg); } header nav ul li a.m8 { width:85px; } header nav ul li.current a.m8 { background-image:url(../images/m5-act.jpg); } #content .inside { padding:22px 20px 0 29px; } #content .inner_copy, #content .inner_copy a { border:0; float:right; background:#000; color:#ff7b01; width:100%; line-height:10px; font-size:10px; margin:-50% 0 0 0; padding:0; } #search-form .rowElem { height:28px; position:absolute; right:0; top:30px; } #search-form input { width:205px; background:#fff; padding:4px 5px 4px 5px; border:1px solid #d0d0d0; border-right:0; color:#454545; line-height:1.2em; float:left; height:18px; } #search-form a { text-transform:uppercase; color:#fff; text-decoration:none; float:left; background-color:#0087c1; padding:6px 11px 0 11px; height:22px; } #search-form a:hover { background-color:#47b6e5; } #newsletter-form { padding-bottom:45px; } #newsletter-form fieldset { background:url(../images/newsletter-bg.gif) no-repeat left top; width:219px; height:131px; overflow:hidden; } #newsletter-form .rowElem { padding:13px 19px 0 17px; } #newsletter-form input { border:1px solid #0086b5; background:#fff; color:#8e8e8e; font-size:.857em; padding:2px 5px 2px 5px; width:171px; margin-bottom:13px; } #newsletter-form h2 { color:#fff; margin-bottom:13px; } #newsletter-form a.fleft { color:#fff; position:relative; top:3px; } #newsletter-form a.fright { display:block; color:#fff; border:1px solid #26b6e8; text-decoration:none; background-color:#006caa; padding:2px 11px 3px 11px; } #newsletter-form a.fright:hover { background-color:#47b6e5; } #contacts-form { clear:right; width:100%; overflow:hidden; } #contacts-form fieldset { border:none; float:left; } #contacts-form .field { clear:both; height:30px; } #contacts-form .field.extra { height:268px; } #contacts-form label { float:left; width:123px; font-weight:bold; color:#008cc4; } #contacts-form input { width:277px; padding:2px 0 2px 3px; border:1px solid #d1d1d1; color:#70635b; } #contacts-form textarea { width:503px; height:252px; padding:2px 0 2px 3px; border:1px solid #d1d1d1; color:#70635b; overflow:auto; } footer { background:url(../images/footer-bg.png) no-repeat center top; margin-top:-120px; min-width:940px; } footer .footerlink { margin:0 auto; width:940px; height:60px; padding-top:60px; color:#454545; font-size:.785em; line-height:1.429em; } .footerlink p { margin:0; padding:0; line-height:normal; white-space:nowrap; text-indent:inherit; color:#454545; } .footerlink a { color:#454545; font-weight:normal; margin:0; padding:0; border:none; text-decoration:underline; background-color:transparent; } .footerlink a:hover { color:#454545; background-color:transparent; text-decoration:none; } .footerlink .lf { float:left; } .footerlink .rf { float:right; } a { outline:none; } 选项限制select time, obj, type from someTable order by obj, type desc 查看顶级目录:

find

使用glob可以更简单的方式实现相同的目的:

maxdepth

然后

find . -type d -print0 -maxdepth 1 | LC_ALL=C sort -z
  • 需要双引号来防止分词和通配
  • 反引号中的
  • dirs=(*/) # store all top level directories into the dirs array dirs=("${dirs[@]%/}") # strip trailing / from each element of the array 可以简单地写成cp -r "$PWD/${dirs[4]}" "$PWD/.." ,不需要创建子shell