LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-28-2010, 11:43 PM   #1
Black.Sands
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Smile What is wrong with this basic shell script?


Hello LinuxQuestions world!

I'm still pretty new to linux, and burning through a unix/linux course offered at the local JC. I'm working on an assignment for shell scripting and I need to make a script that displays a bunch of junk (pwd, date, yadda yadda) and then prompts the user to enter a directory so they can view the contents. If it's valid, then it displays the contents. If it isn't then it throws an error message and the script stops. This is done using if, then and else.

The problem is, my script always shoots straight to the else, even if the subdir entered IS a valid subdirectory.

Here's what I have:

Code:
clear
pwd
echo Today is 'date +%m/%d/%y'
date +%r
echo "Which Subdirectory would you like to display?"
read subdir
if test -d $subdir
then
ls /cs41s1003/$subdir
else
echo "Sorry, $subdir is not a valid subdirectory! Give it another shot!"
fi
When I run the the script

Code:
./Assigment-3
I get:

Code:
/home/cs41s1003/Assignment-3
Today is date +%m/%d/%y
09:42:26 PM
Which Subdirectory would you like to display?
Assigment-3
Sorry, Assigment-3 is not a valid subdirectory! Give it another shot!
[cs41s1003@cc-ccs Assignment-3]$
Can anyone give me a heads up about what I'm fudging here?

Also, cs41s1003 is my "home" folder for the class on our linux server, which is a subdir of home. (home/cs41s1003/Assignment#/yadda/yadda)

Thanks!

Tristan

Last edited by Black.Sands; 03-29-2010 at 12:45 AM.
 
Old 03-28-2010, 11:51 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Please post the output of the script. It's easier to read if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 03-29-2010, 01:37 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Quote:
ls /cs41s1003/$subdir
dir does not exist; you want /home/cs41s1003/$subdir ; that's an absolute path ie parsed as starting from the system(!) root dir aka '/'
 
Old 03-29-2010, 04:13 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Also, your test is against the current directory structure so:

Quote:
if test -d $subdir
This is saying look in . (the directory you started in) for the directory you typed in.
So unless you have:

Quote:
/home/cs41s1003/Assignment-3/Assignment-3
Then it won't work either, ie the test will always be false.
 
  


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
Need some help with a basic shell script trist007 Programming 6 07-22-2008 10:22 AM
Basic Shell Script Help Vasquez Linux - Newbie 3 02-28-2005 08:14 PM
basic shell script help lin00b Linux - Newbie 2 10-09-2004 12:32 AM
basic shell script help coyote399 Linux - Newbie 6 04-14-2004 10:22 PM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 06:34 PM


All times are GMT -5. The time now is 06:25 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration