Sponsored Content
Top Forums Shell Programming and Scripting Help with Shell Script displaying Directories Post 302309608 by cfajohnson on Wednesday 22nd of April 2009 10:54:59 AM
Old 04-22-2009
Quote:
Originally Posted by cjnd1988
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.

Which line is giving you the problem?
Quote:
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:
if [ $# -gt 0 ]
then
  for dir
  do
    descenddir "$dir"
  done
else
  descenddir "$PWD"
fi

Quote:
Code:
#!/bin/bash

displaydir()
{
   n=0
   local alist=$( ls $1 )
   for file in $alist


That will break if any filenames contain spaces. Use:

Code:
for file in "$1"/*

Or cd into the directory:

Code:
cd "$1"
for file in *

Quote:
Code:
   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()
{


It would make things simpler if you execute descenddir() in a subshell:

Code:
descenddir()
(

)

Then you don't need to worry about returning to a previous directory.
Quote:
Code:
   local BASEDIR=$1
   local LOCALDIR="$PWD"

   echo "The present working directory is: $BASEDIR "

   cd $BASEDIR


That will break if $BASEDIR contains spaces. Quote the variable and check that it succeeded:

Code:
cd "$BASEDIR" || return 1

Quote:
Code:
   echo "Do you want to display the contents of this directory?"
   read answer
   case $answer in
     "Y") displaydir "$BASEDIR";;
     "N") ;;
   esac

Code:
   case $answer in
     [yY]) displaydir "$BASEDIR";;
   esac

Quote:
Code:
   local dirlist=$( ls )

   for directory in $dirlist


That will break if any filenames contain spaces. Use:

Code:
for file in *

Quote:
Code:
   do
      if [ -d $directory ]
      then {


There's no need for the braces.
Quote:
Code:
    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

Code:
   case $answer in
      [dD]) descenddir "$BASEDIR/$directory/";;
      [lL]) displaydir "$BASEDIR/$directory/";;
      [Ii]) echo "The directory was ignored" ;;
   esac

Quote:
Code:
 
      }
      fi

   done

   cd $LOCALDIR


That will break if $LOCALDIR contains spaces. Quote the variable and check that it succeeded:

Code:
cd "$LOCALDIR" || return 1

Quote:
Code:
}

if [ $1 ]
   then descenddir $1


That will break if $1 contains spaces. Quote the variable:

Code:
   then descenddir "$1"

Quote:
Code:
   else descenddir "$PWD"
fi

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script to delete directories...

Hi. I'm trying to write a script that will delete all directories found, that are not named as a "number" (year)... here is what i mean, let's say i have within /data/exports the following directories: /data/exports/2000 /data/exports/2001 /data/exports/2002 /data/exports/daily/2000... (5 Replies)
Discussion started by: Stephan
5 Replies

2. Shell Programming and Scripting

shell script to delete directories...

*Just realized that i posted this in the wrong forum. should have been in Shell, though it is on AIX... Hi. I'm trying to write a script that will delete all directories found, that are not named as a "number" (year)... here is what i mean, let's say i have within /data/exports the... (8 Replies)
Discussion started by: Stephan
8 Replies

3. UNIX for Dummies Questions & Answers

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... (1 Reply)
Discussion started by: cjnd1988
1 Replies

4. Shell Programming and Scripting

Displaying uppercase in shell script

Hi, The user value y/n should get display in upper case printf "$FBOLD\nDo you want to continue?: $FREG" Do you want to continue?: y Though user enters in smaller case y, but it should get display in uppercase Y How it can be done ? With Regards (2 Replies)
Discussion started by: milink
2 Replies

5. Shell Programming and Scripting

renaming directories with shell script

Hi All after looking around the website and various other resources I become stuck. I'm trying to rename directories from Firstname Initial Lastname to lastname,_firstname_initial so far ive got for f in {./} do rename -n 'y/A-Z/a-z/' * rename -n 's/\ /_/g' * ... (2 Replies)
Discussion started by: harlequin
2 Replies

6. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

7. Shell Programming and Scripting

Shell script to modify file in several directories

Hi, I want a script shell to automate modifying httpd.conf file for several instances of apache, save httpd.file before changing it, after modifying it and then restart apache. - Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories : "... (4 Replies)
Discussion started by: bras39
4 Replies

8. Shell Programming and Scripting

creating multiple sub-/directories using a shell script

0 Hi, I am looking for a way of creating multiple directories using the mkdir -p command in a shell script. I'm working with an Ubuntu machine and try to do something like that: #!/bin/sh ... (3 Replies)
Discussion started by: frymor
3 Replies

9. UNIX for Dummies Questions & Answers

Shell Script for displaying the line till the target word

" Script for display sentences with special character" Hi, Could any one share a command how to display a line until my target word. For ex: My file has the content as: select * from db_wrk where col1 < col2 insert into table_name values('1','2','tst','wrk','dev','prod') My target... (10 Replies)
Discussion started by: Kalaiselvi66
10 Replies

10. Shell Programming and Scripting

Displaying Column header in shell script

Hi, I need to display specific columns using select statement and spooled to a file and sending it as e-mail. But i am not seeing column header in my output even i use SET HEADING ON.//PREDEFINED LOGIN DETAILS ${ORACLE_HOME}/bin/sqlplus -s ${DB_LOGIN}/${DB_PASSWD} <<EOF SET FEEDBACK OFF SET... (1 Reply)
Discussion started by: pvelmuru
1 Replies
All times are GMT -4. The time now is 09:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy