LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-10-2009, 05:33 AM   #1
adilwaheed
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
Question Passing directory name as a command line argument


Hi!
I want to pass a directory name as a command-line argument and display attributes of all files in that directory.
Kindly tell me how to write script for this function.
 
Old 03-10-2009, 05:40 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This looks like homework....

Regardless, you will need to supply some more detail as to what you are doing, and what you have tried.

For example, what attributes are you looking for? Something other than what is supplied by "ls -l"?
 
Old 03-10-2009, 03:25 PM   #3
adilwaheed
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
I have tried

$ ls -l $(read dir) and
$ ls -l `read dir`

but it shows long listing of only root directory.
 
Old 03-10-2009, 03:41 PM   #4
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
arguments for a shell script would be $X and up where X is a number >=1 (o is the command itself the rest are the arguments passed to that argument)

try creating a script script.sh as follows

echo $0
echo $1
echo $2
...
echo $x

then run that script with x number of agruments (any arbatrary numbers or words separated by spaces will work) and see what is output

Last edited by frieza; 03-10-2009 at 03:42 PM.
 
Old 03-12-2009, 03:01 PM   #5
adilwaheed
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for your help. I've written the following script and it worked for me.

#! /bin/bash

if test -d "$1"
then
ls -l $1

elif test -z "$1"

then
echo Incomplete argument...
exit 0


else
echo Directory not found...
exit 0
fi
 
  


Reply



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
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
[SOLVED] pass file-name as command line argument vikas027 Programming 2 10-14-2008 11:52 PM
Perl command line argument noir911 Programming 8 01-15-2008 04:59 AM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 09:51 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

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

All times are GMT -5. The time now is 08:16 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
Open Source Consulting | Domain Registration