LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2011, 11:44 PM   #1
abood1190
LQ Newbie
 
Registered: Aug 2011
Posts: 5

Rep: Reputation: Disabled
question in shell script


Hell guys,
I need a little help to solve this question

Write a Bourne shell script which:
Has one command line argument.
If the command line argument is a directory then the script should output the number of files in the directory.
If the command line argument is an ordinary file then the script should output whether or not the file has execute permission for the file owner.
If the command line argument is neither a file or directory then the script should output an appropriate error message.
If no command line argument is supplied then the script should output an appropriate error message.
 
Old 08-29-2011, 01:16 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello and Welcome to LinuxQuestions,

Nobody here will do your homework for you, that is, if this is homework. It sure looks like that to me. Why not? Because you'll learn a lot more doing it yourself. Of course when you encounter errors are get stuck writing your script, that's when you turn to us for guidance. Basically it all comes down to: Show us what you've got and where it's failing, then you'll get dedicated answers. A good starting point are these guides:
Bash Guide for Beginners
Advanced Bash Scripting Guide

Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric
 
Old 08-29-2011, 01:56 AM   #3
abood1190
LQ Newbie
 
Registered: Aug 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
I tried to solve

this is my code ( its wrong ! )


Quote:
#!/bin/sh

echo " Command line argument is :" $#

if [ -d $1 ]
then
echo " The number of files in the directory is " ls -l | wc -l

if [ -f $1 ]
then
echo " The execute permission for the file owner is" ls $1

else

echo " It is neither a file or directory "

fi

if [ -z $1 ]

echo " There is no command line argument ! "

fi
 
Old 08-29-2011, 02:59 AM   #4
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 201

Rep: Reputation: 36
Hi there.

You missed the else for the first if, besides, you have missed one "fi".

For the number of files of the directory you are counting files AND directories, I don't know if that is the required answer, if you only want files you will have to check the first caracter in the permisions, if it is a "d" it is a directory, not a regular file.

In case of a file your are getting the file ls, no permissions are shown in that command, you should use ls -l to get an output with permissions, besides, question is to retrieve execute permission for the owner, so you will have to get the exact part of the answer retrieved with ls -l.

Use man to know a command's usage and its parameters:

man ls

The commands you might be interested in checking with man are:

cut, grep, awk(swiss knife, you won't need a number of other commands if you know how to use this one)

Last edited by rodrifra; 08-29-2011 at 03:05 AM.
 
Old 08-29-2011, 03:36 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Apart from the syntax errors in the if/then/else construct, What you missed is command substitution to retrieve the output of commands and print them out (or assign them to a variable). Check it here and here.
 
Old 08-29-2011, 07:30 AM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-29-2011, 08:17 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Personally I would go back to your instructor and ask what are the suggested mechanisms to be used and how strict is the testing meant to be.

The reason I say the above is you are using the standard test facility, ie [ is synonymous with test. Both utilise the -d and -f options but should you be testing a symbolic link,
which strictly speaking is neither a file nor a directory, your tests will return true, ie if symbolic link is to a directory and you use -d it will return true as it is testing
what the link is pointing to and not the link itself.

Your order leaves a bit to be desired as well seeing you are testing what is in $1 at the end, plus what if there are more than one arguments??

Lastly, I think you need to look up what $# does as your use does not look correct to me.
 
Old 08-29-2011, 09:57 AM   #8
abood1190
LQ Newbie
 
Registered: Aug 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
rodrifra
colucix
XavierP
grail


Thank you guys for replying, i started to love it

i tried and im stuck with last two points.

this is my work :

Quote:
#!/bin/sh

echo " Command line argument is :" $1

if [ -d $1 ]
then
echo " The number of files in the directory is ` ls -l | wc -l` "
else
echo " It is not a dirctory "
fi

if [ -f $1 ]
then
echo " The execute permission for the file owner is ` ls -l $1`"
else
echo " There is no execute permission "
fi
 
Old 08-29-2011, 10:21 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
I will ask you some questions:

1. Where are your argument test(s)? You have no test now for the number of arguments so if user enters nothing or more than 1 your code will not show an error or exit.

2. You test for a directory but by using 'ls -l' you assume that the are no sub-directories. This should be checked against your requirements.

3. How does doing an 'ls -l' test whether or not a file has the execution bit set?

Lastly, if you use code tags instead of quote tags your formatting of the code will be maintained.
 
Old 08-29-2011, 01:10 PM   #10
abood1190
LQ Newbie
 
Registered: Aug 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I will ask you some questions:

1. Where are your argument test(s)? You have no test now for the number of arguments so if user enters nothing or more than 1 your code will not show an error or exit.

2. You test for a directory but by using 'ls -l' you assume that the are no sub-directories. This should be checked against your requirements.

3. How does doing an 'ls -l' test whether or not a file has the execution bit set?

Lastly, if you use code tags instead of quote tags your formatting of the code will be maintained.
To be honest with you.
I typed my previous code according to my lecture notes !
I know nothing about ls -l and arguments !!
I need help gentlemen ,,,, due date for submission on Wednesday ^_^
 
0 members found this post helpful.
Old 08-29-2011, 03:16 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,570

Rep: Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582Reputation: 4582
Quote:
Originally Posted by abood1190 View Post
To be honest with you.
I typed my previous code according to my lecture notes !
I know nothing about ls -l and arguments !!
I need help gentlemen ,,,, due date for submission on Wednesday ^_^
Ok..then you should be reading the man pages on the "ls" command, which will tell you what you're missing. And the point of an assignment is to make you think, learn, and be able to do the work. If you're just typing it in, and getting us to fix the problems without UNDERSTANDING HOW, you won't learn, and it'll make every assignment from now on, harder. You won't understand this step, so the next steps will be even MORE unclear.
 
Old 08-29-2011, 09:48 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,449

Rep: Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401
Read the links provided by EricTRA in post #2; read this http://rute.2038bug.com/index.html.gz; more accurately search those docs for relevant info/examples.
Also, check the exact usage of each cmd here http://linux.die.net/man/

I suggest you book mark all these links; you are going to need them.
 
1 members found this post helpful.
Old 08-29-2011, 10:13 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Couldn't agree more with TB0ne. There is no pass mark or degree waiting for any of us for passing YOUR assignment.

Also, if you read a little closer through the replies you will see that solution suggestions have been given, plus I am
sure things haven't changed too much since I studied that the teacher / lecturer is asking you to do something
that has not been covered.
 
1 members found this post helpful.
Old 08-31-2011, 07:25 PM   #14
abood1190
LQ Newbie
 
Registered: Aug 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
TB0ne
thats what was i trying to find out
but the problem is most of the things i dont understand it !
Thank you

chrism01
i couldnt find post 2 ,,,
Thank you

grail
Thank you for helping me

the time for submission is up !
Thank you guys
i will try to revise again. i hope i can share my knowledge with you soon ...
 
Old 08-31-2011, 08:52 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Quote:
chrism01
i couldnt find post 2 ,,,
The post where you wrote this was 14 which is the number on the far top right corner of your post.
So look for the one with a 2 in this position
Quote:
the time for submission is up !
Well after it is marked and you get the results and solutions, come back and ask more questions.
 
  


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] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 10:26 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
[SOLVED] Shell script question poorboyiii Linux - General 3 02-13-2010 06:42 PM
Question on Shell Script manickaraja Programming 5 02-12-2010 07:56 AM
shell script question yoderp Programming 6 06-16-2005 05:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:08 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