Help with Accepting Directories as an Argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Accepting Directories as an Argument
# 1  
Old 12-07-2010
Help with Accepting Directories as an Argument

I'm doing a script where you are suppose to start off in accepting one or more directory as an argument. How do i do this?

Sorry for the nub question.
# 2  
Old 12-07-2010
Code:
 
#!/bin/ksh
 
export dir="$1"
 
if [ ! -d "$dir" ]
then
 echo "Not a directory: '$dir'
 
Usage: ${0##*/} <directory_path>
" > &2
fi
 
 .
 .
 .
 .

# 3  
Old 12-07-2010
Code:
#!/bin/ksh
if [ $# -lt 1 ]
then
    echo "Usage: myscript dir [dir ...]" >&2
    exit 1
fi
 
while [ $# -gt 0 ]
do
    dir=$1
    if [ ! -d "$dir" ]
    then
        echo "Not a directory: $dir"
        exit 2
    fi
    .
    .
    .
    shift
done

---------- Post updated at 12:59 PM ---------- Previous update was at 12:41 PM ----------

Down side of above is that it processes directories before all the parameters are checked.

This solution is better, but more complex as it uses an array to store the directories.

Both solutions work fine even if directory names contain one or more spaces:

Code:
#!/bin/ksh
if [ $# -lt 1 ]
then
    echo "Usage: myscript dir [dir ...]" >&2
    exit 2
fi
 
i=0
while [ $# -gt 0 ]
do
    if [ ! -d "$1" ]
    then
        echo "Not a directory: $1"
        exit 2
    fi
    dirs[i++]="$1"
    shift
done
 
i=0
while [ $i -lt ${#dirs[@]} ]
do
   echo Process directory ${dirs[i]} here...
   let i+=1
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Accepting search pattern to script argument

I have a script in tcsh and I want to have a find option to which I can pass a file search pattern I want using the command: /home/chrisd/tatsh/trunk/hstmy/bin/tcsh/raytrac.tcsh -f=*rc* The set command seems to fail when user does not supply a search pattern (if user just supplies -f, a... (2 Replies)
Discussion started by: kristinu
2 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

script accepting password

Hi friends, I am very new to Unix scripting and having some difficulty in my first shell script. I have written a simple shell script to upload an artifact to a remote machine on the network. echo "Uploading the artifact" scp app.war username@remotemochine.domainname.net:/home/deployables... (3 Replies)
Discussion started by: prashdeep
3 Replies

4. Shell Programming and Scripting

Accepting Input regardless of Case

Hi I am trying to get my script to accept input regardless if the person enters a or A. here is the portion of the code where I get the input. echo -n 'Please enter your choice:' # prompt user for input. read reply # read input echo case $reply in #... (2 Replies)
Discussion started by: DualPandas
2 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

Accepting A-Za-Z

Is there a way accept A-Za-z0-9 from the user from a parameter? EX. I want to take the parameter from the user even if its hEu or H3y and store it as a parameter ( $1 ) (18 Replies)
Discussion started by: puttster
18 Replies

7. UNIX for Dummies Questions & Answers

accepting input date

I how do i accept a input date in script which is lesser than a specified day? ex: to accept a date less than or equal to 100 days(from today).?:( Thanks for the help in advance.:) (1 Reply)
Discussion started by: abhi_123
1 Replies

8. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

9. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

10. Shell Programming and Scripting

Not able to display the value I am accepting as an argument

Hi, I am new to UNIX. I am facing some problem here. #! /usr/bin/ksh currDate = $1 export currDate; echo " Date is $currDate" when I run this script, it says : currDate not found. Can anybody point out the mistake please. --mahek (3 Replies)
Discussion started by: mahek_bedi
3 Replies
Login or Register to Ask a Question