你如何存储的目录列表到猛砸一个数组(然后打印出来)? [英] How do you store a list of directories into an array in Bash (and then print them out)?

查看:169
本文介绍了你如何存储的目录列表到猛砸一个数组(然后打印出来)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据多少目录写一个shell脚本来显示由用户,然后用户输入选择索引号的目录中的目录列表有

我想这是某种形式的数组操作,但林不知道如何在shell脚本做到这一点。

例如:

 > whichdir
有在当前路径3显示目录
1 DIR1
2 DIR2
3 DIR3
你要哪个目录?
> 3
您选择DIR3!


解决方案

  $ LS -a
./ ../包含.foo /酒吧/巴兹qux *
$禁用了javascript -s dotglob
$禁用了javascript -s nullglob
$阵列=(* /)
$为DIR中的$ {数组[@]};不要回声$目录; DONE
包含.foo /
酒吧/
$为* DIR /;不要回声$目录; DONE
包含.foo /
酒吧/
$ PS3 =你想要哪个目录?
$回声有$在当前路径{#数组[@]}迪尔斯 \\
在$ {数组[@]}选择目录;不要回声您选择了$ {}目录!;打破; DONE
有2个显示目录中的电流路径
1)包含.foo /
2)酒吧/
你要哪个目录? 2
你选择吧/!

I want to write a shell script to show a list of directories entered by a user and then for a user to select one of the directories with an index number based on how many directories there are

I'm thinking this is some kind of array operation, but im not sure how to do this in shell script

example:

> whichdir
There are 3 dirs in the current path
1 dir1
2 dir2
3 dir3
which dir do you want? 
> 3
you selected dir3!

解决方案

$ ls -a
./ ../ .foo/ bar/ baz qux*
$ shopt -s dotglob
$ shopt -s nullglob
$ array=(*/)
$ for dir in "${array[@]}"; do echo "$dir"; done
.foo/
bar/
$ for dir in */; do echo "$dir"; done
.foo/
bar/
$ PS3="which dir do you want? "
$ echo "There are ${#array[@]} dirs in the current path"; \
select dir in "${array[@]}"; do echo "you selected ${dir}"'!'; break; done
There are 2 dirs in the current path
1) .foo/
2) bar/
which dir do you want? 2
you selected bar/!

这篇关于你如何存储的目录列表到猛砸一个数组(然后打印出来)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆