Need help in listing directories inside korn shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in listing directories inside korn shell script
# 1  
Old 09-07-2011
Question Need help in listing directories inside korn shell script

Hi All,

I need to loop through each item in current path, if it is a direcotry do soemthing, if its a file jsut skip it and move to next item in loop.

Tried

Code:
 
 
if test ! -d $i
then
echo "The current item $i is not a directory"
continue
fi

This doesnt seems to be working . It goes into the if statemenet even if its a directory. Please help
# 2  
Old 09-07-2011
Are you making sure that value in $i has the complete path and not just relative path..? Please post the values of variable i from your script or post the whole loop your trying. Instead of archaic test command you could try [[ ! -d "$i" ]]
# 3  
Old 09-07-2011
Tried using

ls -d */ from within the script , got the below message

*/: No such file or directory


Is there any other workaround solution..
# 4  
Old 09-07-2011
Quote:
Originally Posted by justchill
Tried using

ls -d */ from within the script , got the below message

*/: No such file or directory
The above message is correct. Looks like there is no directory where you do ls -d */. Did you try the same from command prompt rather trying from the script..? Other option would be
Code:
ls -l|sed '/^d/!d'

# 5  
Old 09-07-2011
Hi michaelrozar17,

tried
Code:
[[ ! -d "$i" ]]

but got "[[: not found" message

Below is my code
There aer more than 100 directories in the path ${dirpath}
Code:
 
for dir in ${dirpath}
    do
        LIST="`/bin/ls ${dir}`"
        for i in ${LIST}
        do
            
            if [! -d "$i" ]
            then
                echo "this is not a directory"
                                continue
            fi
----
----

# 6  
Old 09-07-2011
Code:
 
echo ${dirpath} | while read line
do
 find $line -type d # read the directory name here...
done

# 7  
Old 09-07-2011
Quote:
Originally Posted by justchill
Hi michaelrozar17,

tried
Code:
[[ ! -d "$i" ]]

but got "[[: not found" message

Below is my code
There aer more than 100 directories in the path ${dirpath}
Code:
 
for dir in ${dirpath}
    do
        cd ${dirpath} # move to the respective dir and then do ls
        LIST="`/bin/ls ${dir}`"
        for i in ${LIST}
        do
            
            if [ ! -d "$i" ] # Leave space around square brackets
            then
                echo "this is not a directory"
                                continue
            fi
----
----

As you have said ${dirpath} has more than 100 directories, i assume those to be at different location and not just under the directory where the script is stored.
This User Gave Thanks to michaelrozar17 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how do i execute nohup and background job inside the korn script for db2

load_cursor_stmt() { ls /db/admin/ddl/rmn01000/load_cursor*.sql|while read file do echo "${file}" `nohup db2 -tvf "$file" \&` done }Error: ------- /admin/ddl/rmn01000/load_cursor5.sql /db/admin/ddl/rmn01000/load_cursor6.sql + read file + echo... (3 Replies)
Discussion started by: Hangman2
3 Replies

2. Shell Programming and Scripting

listing directories and sub directories with time and name options

Hello all! I'm looking to list directories and sub-directories of a path, on this forum I found this command: find $path -type d -exec ls -ld {} \; The issue I have is that with a simple ls, the list is listed by name, and using -t I get it by time. How could I list directories and sub... (5 Replies)
Discussion started by: nomadvisuals
5 Replies

3. Shell Programming and Scripting

Request for shell script for listing directories, subdirs containing specific files.

I'm looking for a script which outputs the list of directories and sub directories from root level consisting of specific files. For instance I want shell script to list all the directories and subdirectories containing .txt files.:wall: (4 Replies)
Discussion started by: super210
4 Replies

4. Homework & Coursework Questions

Korn Shell Script

1. The problem statement, all variables and given/known data: Write a korn shell script with an alfanumeric string as argument. The script lists the file's names in the current directory that contain the given string as substring and that can be read and written. 2. Relevant commands, code,... (3 Replies)
Discussion started by: burm
3 Replies

5. Shell Programming and Scripting

Korn Shell Script

I have to solve some exercises in Korn Shell, but i'm having some problems. For example: Write a korn shell script with an alfanumeric string as argument. The script lists the file's names in the current directory that contain the given string as substring and that can be read and written. I... (3 Replies)
Discussion started by: burm
3 Replies

6. Shell Programming and Scripting

Sorting Windows Directories using Korn script.

I have a directory called test, which contains multiple sub directories namely TF80A, TF80B, TF80C. I need to sort these directories by name, so in the above case TF80A Is the oldest and TF80C is the latest. Is there a ksh script that would loop through the directories and sort these... (1 Reply)
Discussion started by: amadhani
1 Replies

7. Shell Programming and Scripting

Need script to find errored files inside directories

Hi people. I working on a script to check for files that they are suposed not to be on the directory. I mean, inside of each directory it must have some files but some could be wrong, and i want to move the files that are wrong. Ex: CSPOTGET edpst/CargadoresSPOT Historicos_Spot_MDI.zip... (4 Replies)
Discussion started by: osramos
4 Replies

8. AIX

Help with Korn Shell script

I have this Korn shell script that runs via a cron entry. It runs in a loop "watching" a specific file system for files with a certain name. The file system that it is watching is an upload file system for an FTP server. When files that are the correct name come in, it takes the extension of the... (1 Reply)
Discussion started by: heprox
1 Replies

9. UNIX Desktop Questions & Answers

korn shell script

hi all i am writing the korn shell script. i have a SQL script which gives me the folowing output DSA.WLG.20050713211544.20051025.20050713211544 28991 1130198400 DSA.WLG.20050713211544.20051025.20050713211544 25881 1130198400 DSA.WLG.20050711210100.20051025.20050711210100 25881 ... (3 Replies)
Discussion started by: pavan_test
3 Replies

10. UNIX for Dummies Questions & Answers

korn shell script

hello., i have 2 files.. 1 file is in this folder /home/test/ssk/DSA.WLG.20050713211544.20050710.20050713211544 (this part) other file is in this folder /home/kk/dev/DSA.WLG.20050711210100.20050710.20050711210100 ... (1 Reply)
Discussion started by: pavan_test
1 Replies
Login or Register to Ask a Question