Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.