#!/bin/bash
declare -a dirs
index=0
for dir in `ls -d */`
do
dirs[$index]=$dir
echo $index. $dir
index=`expr $index + 1`
done
Its giving me distorted output:
c.sh: dirs[0]=1/: No such file or directory
0. 1/
c.sh: dirs[1]=11844/: No such file or directory
11844/
c.sh: dirs[2]=12819/: No such file or directory
12819/
c.sh: dirs[3]=15734/: No such file or directory
15734/
c.sh: dirs[4]=16217/: No such file or directory
16217/
How can I fix this
rikxik
March 10, 2009, 10:22pm
2
I get no such error:
$ ls -ltR
.:
total 8
-rwxr-xr-x 1 xxxxx xxxxx 127 Mar 11 02:18 bsh.sh
drwxr-x--- 2 xxxxx xxxxx 96 Jun 25 2008 one
drwxr-x--- 2 xxxxx xxxxx 96 Jun 25 2008 two
./one:
total 0
-rw-r----- 1 xxxxx xxxxx 0 Jun 25 2008 one.rar
-rw-r----- 1 xxxxx xxxxx 0 Jun 25 2008 three.rar
-rw-r----- 1 xxxxx xxxxx 0 Jun 25 2008 two.rar
./two:
total 0
-rw-r----- 1 xxxxx xxxxx 0 Jun 25 2008 five.rar
-rw-r----- 1 xxxxx xxxxx 0 Jun 25 2008 four.rar
-rw-r----- 1 xxxxx xxxxx 0 Jun 25 2008 six.rar
$ cat bsh.sh
#!/bin/bash
declare -a dirs
index=0
for dir in `ls -d */`
do
dirs[$index]=$dir
echo $index. $dir
index=`expr $index + 1`
done
$ bsh.sh
0. one/
1. two/
That is because of you have higher (2.5) version of bash. My issues is that I need to display list of directories under a path and then take the user input to navigate so I need an array else it would become difficult.
Not sure what other solution I can avail
rikxik
March 10, 2009, 10:53pm
4
Can you know my bash version by looking at output of echo?!! Thats kewl man...
$ bash --version
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.