Help displaying contents of Directories - Urgent


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help displaying contents of Directories - Urgent
# 1  
Old 04-20-2009
Help displaying contents of Directories - Urgent

I am new to shell programming and have an assignment question which requires me to list the contents of the present working directory in 4 column format and highlight any subdirectories. It then requires me to develop the shell script to accept a directory name as a positional parameter (if no parameter use the Present Working Directory); then adapt it to accept more than one directory as positional parameters; and finally adapt it to display the name of the sub directory and ask whether to descend and list the contents, list the contents without changing the Present working directory, or ignore the directory.

I am struggling to write a shell script which performs this task. I found the following script in a forum which i think was written for old version shell (SH). I have tried to reuse this shell and adapt it to the Bash shell. If anyone is able to help me either adapt the below code or have any suggestions on how i can go about this question. Please help. Major thanks in anticipation.

#!/bin/sh

if [ -n "$1" ]
then
WORKDIR=$1
else
WORKDIR=`pwd`
fi

WORKDIR=`echo $WORKDIR | awk \
'{
if (substr($0,length($0),1)=="/")
{
print substr($0,1,length($0)-1)
} else {
print $0
}
}'`

echo $WORKDIR

for DIR in `ls -l $WORKDIR | grep "^d" | awk '{ print $NF }'`
do
echo "Found directory $DIR, Decend, List, Ignore (D,L,I) ?"
read DLI
DLI=`echo $DLI | tr "dli" "DLI"`
case $DLI in
"D" ) $0 $WORKDIR/$DIR;;
"L" ) ls -l $WORKDIR/$DIR | grep -v "^d" | awk -v COUNTER=0 \
'{
printf "%s ",$NF
COUNTER++
if (COUNTER>=5)
{
printf "\n"
COUNTER=0
}
}' | column -t;;
"I" ) echo "Ignored directory $DIR";;
esac
done

ls -l $WORKDIR | grep -v "^d" | awk -v COUNTER=0 \
'{
printf "%s ",$NF
COUNTER++
if (COUNTER>=5)
{
printf "\n"
COUNTER=0
}
}' | column -t

regards Clinton
# 2  
Old 04-20-2009
No duplicate or cross-posting, please read the rules.

Continue here:

https://www.unix.com/shell-programmin...#post302308740

Thread closed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Clear contents of specified directories, then return exit status

Hello, this is my first post here. I'm attempting to write a bash shell script to rm the contents of a directory without deleting the directory, specifically in OS X 10.10 . Here's what I have: function clear() { USER="$USER" DIR=$1 rm -rfv /Users/"$USER"/library/$DIR/* } clear... (6 Replies)
Discussion started by: YouNicks
6 Replies

2. Shell Programming and Scripting

**URGENT ** : Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (1 Reply)
Discussion started by: shadow_fawkes
1 Replies

3. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

4. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. UNIX for Dummies Questions & Answers

Displaying contents with Echo

Hello, How can i display the below ... say echo "Start \n Analyse \n ~/temp/input.txt \nStop" Now my output should look like below Start Analyse This is from input file Stop I tried echo "Start \n Analyse \n $var \nStop where $var = "~/temp/input.txt".. It did not work out..... (3 Replies)
Discussion started by: sathyaonnuix
3 Replies

6. Shell Programming and Scripting

compare the contents of two directories

i have been asked to write a bash shell script comparing two directories and sed or awk should not be used in this assignment. compdir will compare filenames in two directories, and list information about filenames that are in one directory but not the other. The information listed will be a long... (1 Reply)
Discussion started by: soccerball
1 Replies

7. Shell Programming and Scripting

Passing filename as parameter and displaying the file contents

Hi All, Its extremely urgent regarding the following requirement. I have created few files in a directory. I have write a program in shell scripting such that it prompts for the filename . once the filename is entered,it should print the contents of the file. Can anyone help with... (7 Replies)
Discussion started by: radhi2424
7 Replies

8. Shell Programming and Scripting

Help with Shell Script displaying Directories

I am new to shell programming and have an assignment question which requires me to list the contents of the present working directory in 4 column format and highlight any subdirectories. It then requires me to develop the shell script to accept a directory name as a positional parameter (if no... (11 Replies)
Discussion started by: cjnd1988
11 Replies

9. Shell Programming and Scripting

Beset Scripting langauge to delete old directories and contents?

What would the best scripting language be for selecting all the directories in the current directory that are over a week old and deleting there contents? Perl? bash? groovy? ruby? something else? How would you do it? I got a start with perl in the beginners@perl.org and I started to think:... (3 Replies)
Discussion started by: siegfried
3 Replies

10. UNIX for Dummies Questions & Answers

Delete Directories and Contents

How to delete all subdirectories and contents? oh, and if they are hard linked? (1 Reply)
Discussion started by: t4st33@mac.com
1 Replies
Login or Register to Ask a Question