Help with Shell Script displaying Directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Shell Script displaying Directories
# 8  
Old 04-22-2009
Thankyou for your quick reply. I have not had a chance yet to try out the changes you have said. I am not exactly sure what line though is giving me the problem. It is something in the display directory function. What happens is:

When the function is called the first thing that would happen is the contents of the directory that was passed would be assigned to the variable alist. This is all good and the contents of the directory are displayed correctly in 4 columns. The problem starts when the function is called for the second time for the next directory. This is what happens. The value that is assigned the the variable alist is just the contents of the second directory passed (which is correct that is what i want). What is displayed though is the contents of the first directory again with the contents of the second directory just added to the end. All i want to be displayed is the contents of the current directory not every directories contents aswell that has been passed to the function before the current one.
It is like the value somewhere hasn't been wiped. I don't know why it is doing this. I had tried doing one of the changes you suggested which was to change to the cd directory and use the * with the for statement (within the display function) but from memory i am pretty sure it was doing the same thing with appending all directories onto the end of each other.

One thing i forgot to ask was when i am displaying the contents in four columns how would you go about highlighting the subdirectories i.e. make them a certain colour or something that differentiates them from files.
# 9  
Old 04-22-2009
Code:
displaydir()
(
  [ -d "$1" ] && cd "$1" || return 1
  w=$(( $COLUMNS / 4  ))
  n=3
  em='\e[31m'
  no='\e[0m'
  for file in *
  do
    n=$(( ($n + 1) % 4 ))
    [ $n -gt 0 ] && printf "\e[$(( $n * $w ))C\e[D "
    if [ -d "$file" ]
    then
       printf "$em%-$w.${w}s$no\r" "$file"
    else
       printf "%-$w.${w}s\r" "$file"
    fi
    [ $n -eq 3 ] && echo
  done
)

# 10  
Old 04-23-2009
Thank-you Thank-you Thank-you. I can not thank you enough for all your help. I have got everything to work now thanks to your help. There is one thing though that i have noticed. When i choose to list the directories; the original directory that is passed to the display function displays perfectly but then when i either descend into the subdirectory then display or just select list the contents of the subdirectory it doesn't seem to always display the contents of the subdirectory. It just doesn't display anything it just moves onto the next subdirectory.

For example if i don't pass a directory as a paramater to the shell when i run it, it uses the present working directory which is my home directory. i just have the default folders and have put a few test txt documents in a couple of the folders. When i run through the shell it displays the home folders contents but it doesn't display anything for the subdirectories. it does not display the few text documents i have placed.

Any thoughts. My code is pretty much the same except i have implemented the changes you suggested in your previous posts.
# 11  
Old 04-23-2009

"Pretty much the same" doesn't tell us anything. If you are still having problems, post the exact code you are now using and explain exactly what is going wrong and what you expect to happen.
# 12  
Old 04-23-2009
Below is the code i now have. OK this is an example. If this shell situated in your home directory and it was run without any parameters it would use the present working directory which is the user/home directory. OK now i get to the stage in the code where it asks if i want to display the contents of the directory i say yes and it displays fine (in four columns highlighting the subdirectories). Now it descends into the home directory and finds the first subdirectory (on my computer it is the documents folder). It then asks you if you want to descend into the folder, list the contents or ignore. If i ask it to list the contents it doesnt display anything it just moves onto the next directory (it is like it doesnt pick up anything in the directory, all i have in the directory is a few text documents so i would have thought they would have been displayed). If i choose the descend option instead it descends into the directory correctly and then it asks the question if you want to display the contents of the directory if i say yes it doesnt display anything either.

That said if i pass a parameter to it say /etc it displays the contents of some of the subdirectories. Some may not have any contents so that is fine. And some of them i don't have permission to view so that is fine. The ones that do display however display correctly. Is it just something with the home directory? or something in the code. Anyway the code is posted below.
Code:
#!/bin/bash

#descend/display/ignore the contents of a chosen directory



#displays the contents of a passed directory parameters in four column format and highlights sub-directories


displaydir()

(
	
	[ -d "$1" ] && cd "$1" || return 1

	w=15

	n=3

	em='\e[31m'

	no='\e[0m'

	for file in *

	do

		n=$(( ($n + 1) % 4 ))

		[ $n -gt 0 ] && printf "\e[$(( $n * $w ))C\e[D "

		if [ -d "$file" ]

		then

			printf "$em%-$w.${w}s$no\r" "$file"

		else

			printf "%-$w.${w}s\r" "$file"

		fi

		[ $n -eq 3 ] && echo

	done

)



#filters through the contents of the passed directory parameter and asks whether to descend into the directory

#(which in turn recalls the function), list the contents of the directory (calls the display function), or just 

#ignore the directory and move onto the next.


descenddir()

(

	local BASEDIR="$1"

	local LOCALDIR="$PWD"



	echo "The present working directory is: $BASEDIR "

	cd "$BASEDIR" || return 1

	echo "Do you want to display the contents of this directory (Y, N)?"

	read answer

	case $answer in

		[yY]) displaydir "$BASEDIR";;

	esac


	for file in *

	do

		if [ -d $file ] 

		then

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

			read answer

			case $answer in

				[dD]) descenddir "$BASEDIR/$file/";;

				[lL]) displaydir "$BASEDIR/$file/";;

				[iI]) echo "The directory was ignored" ;;

			esac
 
		fi

	done

	cd "$LOCALDIR" || return 1

)



#goes through all passed directory parameters and calls the descend function. If no parameters use PWD.

if [ $# -gt 0 ] 

   then 

	for dir

	do

		descenddir "$dir"

	done 

   else 

	descenddir "$PWD"

fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question