Beginner at bash scripting - need help with passing arguments

I at the moment, making a simple bash script, capable of setting up an workspace for me, so i don't have to do it manually.. Problem is though i can't seem to provide the bash script any argument, without running into my error checks, checking for input...

Here is the code:

#!/bin/bash
RED='\033[0;31m'
NC='\033[0m' # No Color


if [[ $1 -eq 0 ]]
	then
		echo -e "${RED}Missing Workspace name! -  Provide a name!${NC}"
	exit 1	
fi

if [[ $2 -eq 0 ]]
	then 
		echo "${RED}Missing path to dataset - SPH files${NC}"
	#exit 1	
fi

if [[ $3 -eq 0 ]]
	then 
		echo "${RED}Missing path to Utt!${NC}"
	#exit 1
fi		

WORKSPACE=$1
PATH_TO_DATASET=$2
PATH_TO_UTT=$3

#Create the folder 

mkdir ../${WORKSPACE}

Here is the output I get:

./workspace_setup.sh start
Missing Workspace name! -  Provide a name!

Where start should be the first argument? - What is going wrong here?

"start" is a string, and you are deploying integer comparison operators. And, use strings for the reference, e.g the empty string. Try = like

if [[ $1 = "" ]]

. Double quote $1 if need be.

what about paths... check 2 and check 3

Hello kidi,

Problem is keyword exit 1 , because in case any of the conditions are getting FALSE then it is directly coming out of your script rather than checking further conditions. So you could remove it, so that it will further go to script and do more checks.

Thanks,
R. Singh

Ohh.. well my issue were more how do check whether $2 and $3 contains a legal path, a path in general

What about paths?

if [[ $2 = "" ]]

and

if [[ $3 = "" ]]

? What would constitute a path? We've got absolute and relative paths, and single directories or entire branches. So anything between / and some/relative/directory/branch could be a path, even start can be a valid path. It up to you to define the desired path structure and construct your check accordingly.

#!/bin/bash

if [ "$#" -ne "3" ]
then
        echo "USAGE : script.sh WORKSPACE DATASET_PATH UTT_PATH"
        exit 1
fi

if [ ! -d $2 ]
then
        echo "Missing Path to dataset"
        exit 1
fi

if [ ! -d $3 ]
then
        echo "Missing Path to Utt"
        exit 1
fi

#create the folder
# your code goes here....

Might I suggest that you use double quotes wrapped round the arguments too. If $1 is null, then the test becomes invalid and you may get unexpected errors. You can count the arguments as suggested above and use tests such as:-

if [ -z "$1" ]
then
    echo "First argument is null"
fi

Mixing variable types and operators is usually a bad idea. You need to ensure you compare numbers with numbers using numeric operators and strings with strings with string operators. You can compare numbers using string operators or compare strings using numeric operators, but the results can be unexpected. For instance if you compare 1234 with 321, in a string comparison, 321 is considered larger because it starts with a 3.

You should not really quote numerics.

I hope that this helps,
Robin

1 Like