Sponsored Content
Full Discussion: Chose option Utility
Top Forums Shell Programming and Scripting Chose option Utility Post 302201228 by mschwage on Sunday 1st of June 2008 10:00:04 AM
Old 06-01-2008
Quote:
Originally Posted by balamv
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.
...
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 see you have some of your answers already, but just to make sure this is crystal clear:

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

...is kind of tricky for a new person. It was years before I learned it. Here's the explanation:

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*

. What happens when you type that command to the shell? A number of things:
  1. The shell analyzes your line, looking for metacharacters
  2. It finds the asterisk, which is attached to the f
  3. It looks at all the entries (files and directories) in your current directory, and does a pattern match. All files that begin with f followed by anything (or nothing at all) are matched.
  4. The results of that pattern match become the arguments to "grep"
Notice: the shell hands the argument list to grep. Grep never sees an asterisk.. You need to be aware of how the shell processes command lines.

Now, back to the for loop: Here-
Code:
for entry in *

the asterisk matches everything in your current directory. In this example, it will match
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

Next, I test each one of those entries and, if it IS a directory, append it to the "dirs" variable. So when all is said and done, dirs will look like:
Code:
not_using_0 dir1 dir2

...a list of 3 items, in this case. Every entry in dirs, starting with item 1 (not item 0), is a directory in the current directory.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What utility do I use for this?

I want to pull out the 3rd column of information and stick in a file. What is the Utility I use to do this? (8 Replies)
Discussion started by: James
8 Replies

2. Programming

MAKE utility

I wrote a makefile, every thing is working fine, But One of the C header files which is created by me is kept in a different folder other than the current directory, I have given this PATH to VPATH Variable Example :- VPATH = /home/user1/projects/victor.h It gives an error as : file... (4 Replies)
Discussion started by: victorvvk
4 Replies

3. Shell Programming and Scripting

Gunzip utility

Hi All, Is there any utility or command to read a .gz file without GUNZIPing it? Why I need this because of the huge size of the file. I am looking for something like zcat. Any help is aprreciated. Thanks in Advance.. Regards, rin... (1 Reply)
Discussion started by: rinku11
1 Replies

4. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

5. Shell Programming and Scripting

utility

hi experts, Can you please help me out in removing delimiters with in double quotes from a CSV file. input: ===== a,"bnn,",dgd, "sagfh,dj",ad output ===== a,"bnn",dgd, "sagfhdj",ad there are so mnay fileds in a row and there are millions of rows. Thanks in an advance.... (6 Replies)
Discussion started by: subhendu81
6 Replies

6. Shell Programming and Scripting

How to Unzip a file using unzip utility for files zipped without zip utility ?

Hi, I need to zip/compress a data file and send to a vendor. The vendor does have only unzip utility and can accept only .ZIP files. I do not have zip utility in my server. How do I zip/compress the file so that it can be deflated using unzip command ? I tried gzip & compress commands, but... (1 Reply)
Discussion started by: Sabari Nath S
1 Replies

7. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

8. Shell Programming and Scripting

How to chose certain character in a word?

Hi Expert, I would like to know on how to export only the first 6 character of below 0050569868B7 ABCDEFGHTY to 005056 ABCDEF Thank you. Reggy (7 Replies)
Discussion started by: regmaster
7 Replies

9. Shell Programming and Scripting

Chose list of sub directories in a bash script file

Hi, I have a command in my bash script, searchDirectoryName.sh: DIR_NAME=$(find . -type d) echo "${DIR_NAME}" . ./Dir1 ./Dir1/1.0.2.1 ./Dir2 ./Dir2/1.1 ./Dir3 ./Dir3/2.2.1 How can I select only following directory names with second subdirectoies and without first ./ in the... (3 Replies)
Discussion started by: hce
3 Replies

10. Solaris

Unrecognized option: sparc-sun-Solaris2.10/bin/as: unrecognized option `-m32'

Hi, I installed some packages required by an app built with python. But when I try python setup.py install, I get the following error: /opt/csw/lib/gcc/sparc-sun-solaris2.10/5.2.0/../../../../sparc-sun-solaris2.10/bin/as: unrecognized option `-m32' Could anyone tell me what's wrong... (4 Replies)
Discussion started by: Kimkun
4 Replies
All times are GMT -4. The time now is 12:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy