LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2013, 04:14 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201
Blog Entries: 3

Rep: Reputation: 37
show the directory content using read command


I am trying to write a script in which it should display the content of a directory in terminal when I write the directory name in the terminal using read command.

Suppose first it shows
A
B
C
D
all four are directory, now it asks me to enter the directory name, I enter C
Now it should show the content of directory C or if I enter B, it should show the content of directory B like
B1
B2
B3
B4
Now it asks me again to enter the directory name, I enter B4, now it should show the content of directory B4 and so on.
I have written a script but it is not worth because I am not able to use the loop, I am facing problem in it. I have to make manual entry for it.
Code:
#!/bin/bash
ls -l /media/DGHI-KJHL-021/folder1 | sed 's/  */ /g' | cut -d' ' -f9
echo "Enter the name of the directory, it is case sensitive"
read directory
ls -l /media/DGHI-KJHL-021/folder1/$directory | sed 's/  */ /g' | cut -d' ' -f9
echo "Enter the name of the directory, it is case sensitive"
read directory1
ls -l /media/DGHI-KJHL-021/folder1/$directory/$directory1 | sed 's/  */ /g' | cut -d' ' -f9
read directory2
ls -l /media/DGHI-KJHL-021/folder1/$directory/$directory1/$directory2 | sed 's/  */ /g' | cut -d' ' -f9
I have to use absolute path because I have to run this script on remote server.
 
Old 10-12-2013, 05:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
It isn't clear to me what your ultimate goal is. The above only seems to focus on changing directories and showing possible content, it doesn't mention anything about what you want to do once this is accomplished. With that in mind, have a look at this:
Code:
#!/bin/bash
clear
baseDir="/media/DGHI-KJHL-021/folder1"
cd "${baseDir}"

function showdirs () {
  find . -maxdepth 1 -type d | \
  sed -e '1d' -e 's%^\./% -> %' | \
  sort
}

function listDir () {
  echo ""
  echo "Content of $(pwd):"
  ls -l
  echo ""
}

while true
do

  echo "Available directories in $(pwd):"
  showdirs
  echo ""
  
  read -r -p "Enter directory (l to list / q to quit) : " chDir
  [[ "$chDir" == "q" ]] && exit 0
  if [[ "$chDir" == "l" ]] 
  then    
    listDir
  elif [[ ! -d "$chDir" ]] 
  then 
    echo "Directory not present, try again..."
  else
    cd "$chDir"
  fi
done

exit 0
The above code snippet does the following:
- Start at the given baseDir,
- Show directories present,
- Asks the user to:
--- enter a directory to cd into (you can use .. to go back one dir) -or-
--- press l to list content of current directory -or-
--- press q to quit

The above code is a rough example, but it should handle directory names that contain space as well.
 
1 members found this post helpful.
Old 10-12-2013, 06:06 AM   #3
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
Thank you druuna, I knew if once you put your hand in the thread, it becomes golden thread (a life time preserved). I was writing my basic Idea but it was making question too long.
My task is :
User should enter the name of sub-directory in terminal and it should display the contents (files & directories both) of that sub-directory. If user enters the file name, it should display it's content if it is a text file. If the file is audio, video, image or any other file whose content could not be shown using cat command, there should be message like "could not be opened" and if it is text file (which can be opened in text editor), it should display the content of the file.

You have written the script in an expert way.

Thank you very much.
 
Old 10-12-2013, 06:36 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by bloodstreetboy View Post
My task is :

User should enter the name of sub-directory in terminal and it should display the contents (files & directories both) of that sub-directory.

If user enters the file name, it should display it's content if it is a text file. If the file is audio, video, image or any other file whose content could not be shown using cat command, there should be message like "could not be opened" and if it is text file (which can be opened in text editor), it should display the content of the file.
My previous post gives you a framework you can use to do all the above, I'm sure you'll report back when you get stuck.

Tip: Determining the type of a file (text or binary) can be tricky depending on how precise you want/need to be.
My first approach would be using the file command. Try to keep this as simple as possible by looking for these keywords in the output: ASCII or script. If one of these keywords is found then the file is readable, otherwise it is a binary file.
 
Old 10-15-2013, 01:08 AM   #5
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
Hello druuna,
I have implemented this script and taken it to the next level. I have implemented previous directory functionality as well as after reaching to the particular directory, I have prevented user to reach into it's parent directory. Right now I have to use this script for txt files only so right now it is quite easy to show txt files only using grep and apply cat on them.

I have just a question, can I use dialog box for this script? so it will look more user friendly.
User will select directories using up and down arrows and press return key to perform an action as well as they will open text files using same process too.

Till now what I have done, I have created a menu but if I select first option, how to show directory or files listing in the dialog box.
I have found a script here and modified it according to my requirement.
http://bash.cyberciti.biz/guide/A_menu_box
Code:
# set infinite loop
#
while true
do
 
### display main menu ###
dialog --clear  --help-button --backtitle "dialog box" \
--title "[ M A I N - M E N U ]" \
--menu "You can use the UP/DOWN arrow keys, the first \n\
letter of the choice as a hot key, or the \n\
number keys 1-9 to choose an option.\n\
Enter your choice " 20 120 8 \
sub-dir "To see sub-directories" \
text-list "To see text files" \
pre-dir "To reach into the previous directory" 2>"${INPUT}"
 
menuitem=$(<"${INPUT}")
 
 
# make decsion 
case $menuitem in
	sub-dir) subdir;;
	text-list) textlist;;
	pre-dir) predir;;
esac
 
done
I am working on these functions but not able to show list in the dialog box as well as not able to select directories or files using arrows.

Please give me direction to put dialog box on your script. What should I do to implement it?
Thanks

Last edited by bloodstreetboy; 10-15-2013 at 01:10 AM.
 
Old 10-15-2013, 03:23 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by bloodstreetboy View Post
I have just a question, can I use dialog box for this script? so it will look more user friendly.

Please give me direction to put dialog box on your script. What should I do to implement it?
I'm not sure I'm able to help you with this one. I've never had the need/urge to use dialog before. One, for me important, reason being that dialog isn't installed by default (at least on the distro's I work with).

I rather use the tools that are standard available to create menu's and output. Echo, print, printf and in rare cases tput are all I need when writing shell scripts.

BTW: Ater a quick glance at the dialog tutorial you provided I do believe you should use fselect to work with files and directories.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is there a command like "read" that doesn't show the input? TITiAN Linux - Security 5 03-15-2012 09:29 PM
Evolution does not show content of inbox or sent but does show other folders manny_borges Linux - Newbie 1 05-07-2011 05:08 PM
Dovecot not able to show me mailbox content lmcilwain Linux - Software 3 12-10-2006 03:07 PM
command to show full directory path cuco76 Linux - Newbie 2 10-12-2006 08:36 PM
interactive show/hide content 0raven0 Programming 0 01-15-2006 08:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration