Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-10-2009, 05:33 AM   #1
LQ Newbie
Registered: Mar 2009
Posts: 3

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

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
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
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
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: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
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 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
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"
ls -l $1

elif test -z "$1"

echo Incomplete argument...
exit 0

echo Directory not found...
exit 0


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 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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration