LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 06: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, 06:40 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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, 04: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, 04: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,111

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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 04:42 PM.
 
Old 03-12-2009, 04: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


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


All times are GMT -5. The time now is 03:09 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration