0

I'm having trouble taking in a path where to run the script as a command line argument, test is it exists, then changing to that path to perform work. Here what I'm trying:

#!/bin/bash scriptpath=$1 if [ $# -lt 1 ] then echo "Usage: script.sh <directory_name>" fi if [ -d scriptpath ] then # work...... else echo "Directory does not exist" fi 
1
  • Even if the directory exists flow falls to the else statement Commented Aug 10, 2012 at 21:26

1 Answer 1

4

Change this:

if [ -d scriptpath ] 

to this:

if [ -d $scriptpath ] 

Also, I recommend making use of "", so that your script still behaves properly when the argument contains weird characters. (Unix allows spaces, newlines, asterisks, even control characters inside filenames.) So:

scriptpath="$1" ... if [ -d "$scriptpath" ] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.