The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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
php script + not displaying webpage JamesGoh Web Programming, Web 2.0 and Mashups 1 03-04-2009 06:01 PM
Recursive call to find files and directories in Shell script from current path. Ramit_Gupta Shell Programming and Scripting 2 10-07-2008 04:33 AM
Displaying Array Elements in Shell Scripts ananddr Shell Programming and Scripting 2 09-16-2008 07:53 AM
shell script to delete directories... Stephan Shell Programming and Scripting 8 05-18-2008 11:00 AM
shell script to delete directories... Stephan Shell Programming and Scripting 5 05-16-2008 08:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-20-2009
cjnd1988 cjnd1988 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 8
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 parameter use the present working directory); then adapt is 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. (it was suggested that a recursive call would be handy)

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.

Code:
#!/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

Last edited by Franklin52; 04-21-2009 at 03:54 AM.. Reason: adding code tags
  #2 (permalink)  
Old 04-20-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by cjnd1988 View Post
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.

Homework assignments are not permitted here, but maybe this will get you started:

Code:
unset files
n=0
for file in *
do
  [ -d "$file" ] &&
     files[${#files[@]}]=$file/ ||
     files[${#files[@]}]=$file
done
w=$(( $COLUMNS / 4 - 1 ))
printf "%-$w.${w}s %-$w.${w}s %-$w.${w}s %-$w.${w}s\n" "${files[@]}"
Quote:

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 is 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. (it was suggested that a recursive call would be handy)

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.

With very few exceptions, scripts written for sh will run without modification in bash.
Quote:
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 put code inside [code] tags.
Quote:
Code:
#!/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
  #3 (permalink)  
Old 04-21-2009
cjnd1988 cjnd1988 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 8
Thank - you very much for your reply and thanks for the starting point. Could you please just briefly explain what the following lines are doing just so i fully understand.

Code:
for file in *
do
  [ -d "$file" ] &&
     files[${#files[@]}]=$file/ ||
     files[${#files[@]}]=$file
done
  #4 (permalink)  
Old 04-21-2009
cjnd1988 cjnd1988 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 8
how to test if a directory or not?

I have got the following segment of code. It is going through each file/directory in the present working directory and asking whether it should descend into the directory. If deciding to descend it recalls the shell script with the new directory as a parameter. This is probably not the most efficient way of doing/writing this code but it seems to work. What i want to do is though, i want to only execute this code if it is a directory not if its a file. How do i test for if its a directory before descending into the code.
Code:
echo "The present working directory is: $WORKDIR "
cd $WORKDIR

for directory in *
do
    (I am thinking the test will go in here to test the value of $directory)
    echo "Found directory $directory; do you want to Descend, List or Ignore:"
    read answer
    case $answer in
    "D") WORKDIR=$WORKDIR/$directory
           echo $WORKDIR
           exec ./tryc $WORKDIR;;
     "L") (this will call a function to list the contents of the directory);;
     "I") echo "The directory was ignored" ;;
     esac
done
[/QUOTE]
  #5 (permalink)  
Old 04-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Use test -d.

Code:
if [ -d "$directory" }
then
   echo "$directory is a directory"
else
   echo "$directory is not a directory"
fi
Or you can bypass the test altogether; this will only list directories:

Code:
for directory in */
  #6 (permalink)  
Old 04-22-2009
cjnd1988 cjnd1988 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 8
I have got my coding to the following. So far the code calls a function which is used to go through the contents of the directory and ask whether to descend (which recalls the function with the new directory), list the contents of the directory (which calls the another function designed to list the contents).
My problem is with the listing of the contents. Displaying the contents of the first directory is fine. When i go to display the contents of the second directory, what is displayed is the contents of the first directory with the contents of the second directory appended to the end. I do not want this to happen, i only want it to display the contents of the current directory only.
What have i done wrong to make it do this.

Also at present my shell is only set up to accept one parameter, how would i go about setting it up to accept multiple parameters. Use a while loop maybe around the calling functions incrementing the parameter number??
Code:
#!/bin/bash



displaydir()

{
	n=0

	local alist=$( ls $1 )

	for file in $alist

	do

	[ -d "$file" ] &&

		files[${#files[@]}]=$file/ ||

		files[${#files[@]}]=$file

	done

	w=10
 #$(($COLUMNS/4-1))

	printf "%-$w.${w}s %-$w.${w}s %-$w.${w}s %-$w.${w}s\n" "${files[@]}"
	
}


#unset files

#unset answer



descenddir()

{
	local BASEDIR=$1

	local LOCALDIR="$PWD"


	echo "The present working directory is: $BASEDIR "

	cd $BASEDIR

	echo "Do you want to display the contents of this directory?"

	read answer

	case $answer in

	"Y") displaydir "$BASEDIR";;

	"N") ;;

	esac



	local dirlist=$( ls )

	for directory in $dirlist

	do

		if [ -d $directory ]
 
		then {

			echo "Found directory $directory; do you want to Descend, List or Ignore (D,L, I): "

			read answer

			case $answer in

			"D") descenddir "$BASEDIR/$directory/";;

			"L") displaydir "$BASEDIR/$directory/";;

			"I") echo "The directory was ignored" ;;

			esac
 
		}

		fi

	done

	cd $LOCALDIR

}



if [ $1 ]
 
   then descenddir $1
 
   else descenddir "$PWD"

fi
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0