![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| option followed by : taking next option if argument missing with getopts | gurukottur | Shell Programming and Scripting | 2 | 03-17-2008 12:46 PM |
| -n option | ravi raj kumar | Shell Programming and Scripting | 1 | 01-03-2008 09:20 AM |
| cc option | kuldeep_bora | High Level Programming | 4 | 06-02-2006 09:38 AM |
| -h option | Raom | UNIX for Advanced & Expert Users | 1 | 10-26-2005 01:19 AM |
| cut -f option | 435 Gavea | UNIX for Dummies Questions & Answers | 1 | 11-10-2003 05:50 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Hi,
I am learning unix shell scripting. I want to create a utiltiy. I will explain about that. When I run my utility, it will display only the directories of a particular directory like the below output. For example My current directory is /home/balamv and it has 3 directories called Andrew, Jason, Malar. Hence the output is 1. Andrew 2. Jason 3. Malar Chose option : 2 Processing . . . The main thing here is, this should be generic and if I run this from any directory and it should display all the directories present and also the number sequence. If 20 directories are there, then the options will come till 20. Anyone could pls help me on this? Thanks in advance. |
|
||||
|
You can use arrays in either ksh or bash. I don't have ksh in front of me, so here's bash. Note that this assumes your directory names don't have spaces in them:
Code:
cd /path/to/your/dirs
dirs="not_using_0"
for entry in *; do
[ -d "$entry" ] && dirs="$dirs $entry"
done
dirarray=($dirs)
index=1
while [ -n "${dirarray[$index]}" ] ; then
echo "${index}: ${dirarray[$index]}"
index=`expr $index + 1`
done
while [ -z "$ans" ] ; do
echo -n "Choose one -> "
read ans
[ -z "${dirarray[$ans]}" ] && echo "Invalid response; try again."
done
echo "Result is ${dirarray[$ans]}"
|
|
||||
|
Thanks a lot mschwage. This is my first question in this forum and I have got a quick reply. Thanks for the team working on this.
mschwage, I really dont know how to use the arrays in shell script. Do you have any link to study about this? Thanks for your help on this. And I have below doubts in your ans, 1. dirs="not_using_0" ?? What is this ? Are you declaring dir as variable and why u assign the value "not_using_0" 2. What this is doing for entry in *; do [ -d "$entry" ] && dirs="$dirs $entry" done I am sorry for asking these simple doubts also but I am new to the shell and scripting techs. |
|
||||
|
mschwage, It works Great
But I can't unerstand why u gave the value of "not_using_0 to dirs, as balamv asked ?? balamv,about the for statement, it test all items in the current directory if it is a directory, add it's value to dirs, and so on. Note: replace then with do @line 8 to work properly. |
|
||||
|
I got the answer of my and balamv question, "not_using_0" was assigned to dirs in the first position, and the index was began from the position 1, so it is no matter what is the first element, as we move directly to the second one.
For Example: arrayname[value1 value2 value3] to get the value1 echo ${arrayname[0]} to get the value2 echo ${arrayname[1]} and so on |
|
||||
|
Quote:
First, I recommend programming in ksh. Bash is nice, but it may not be available on all architectures that you may work with. Ksh is more portable and has enough programming features to keep you happy for a while. For using arrays, just do a Google search on "unix ksh array" or "unix bash array". For the ksh one, this link- the first one that came up- looks good: Korn Shell Arrays - Assigning and Accessing Values - Part I Next: Starting with "0" as the beginning element of a list or array is a programmer's thing. End users don't usually count starting with 0. So I make sure to fill the 0'th element of the array with something silly that will never be used. Finally, this: Code:
for entry in *; do
[ -d "$entry" ] && dirs="$dirs $entry"
done
Let's say you do an "ls" in your home directory: ls And let's say you have 2 files and two subdirectories. So the results of ls may be: dir1 dir2 file1 file2 Now- I'm going to get off track for a second- if you wanted to look for the string "mschwage" in all your files, you might do something like Code:
grep mschwage f*
Now, back to the for loop: Here- Code:
for entry in * dir1 dir2 file1 file2 Each one of those entries then, is assigned to the variable "entry", one by one, for each cycle through the loop. So after the shell expands the asterisk, the line looks like: Code:
for entry in dir1 dir2 file1 file2; do Code:
not_using_0 dir1 dir2 |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|