Beginner at bash scripting - need help with passing arguments

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Beginner at bash scripting - need help with passing arguments
# 1  
Old 08-25-2016
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:
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:

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

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

Last edited by kidi; 08-25-2016 at 09:29 AM..
# 2  
Old 08-25-2016
"start" is a string, and you are deploying integer comparison operators. And, use strings for the reference, e.g the empty string. Try = like
Code:
if [[ $1 = "" ]]

. Double quote $1 if need be.
# 3  
Old 08-25-2016
what about paths... check 2 and check 3
# 4  
Old 08-25-2016
Quote:
Originally Posted by kidi
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

Last edited by RavinderSingh13; 08-25-2016 at 09:56 AM..
# 5  
Old 08-25-2016
Ohh.. well my issue were more how do check whether $2 and $3 contains a legal path, a path in general
# 6  
Old 08-25-2016
Quote:
Originally Posted by kidi
what about paths... check 2 and check 3
What about paths?
Code:
if [[ $2 = "" ]]

and
Code:
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.
# 7  
Old 08-26-2016
Code:
#!/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....

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Beginner Bash Scripting Question

Hello, I am new to Linux and studying to become a Unix System Admin. I am taking a course in which I was practicing creating a bash script to ping a particular IP address. The script can be found below: #/bin/bash echo "Enter the IP address" read ip if then ping -c 1 $ip if ;... (3 Replies)
Discussion started by: shah9250
3 Replies

2. Shell Programming and Scripting

Shell scripting with passing arguments

Hi All, I am using the script for creating local queue and passing the arguments while running the script as below n=0 while do e=`expr $n + 3` echo 'DEFINE QL('$e') MAXDEPTH('$6') MAXMSGL('$7') DEFPSIST('$8') '$9'' | /apps/mqm_opt/bin/runmqsc $2 n=`expr $n + 1` done Running the... (5 Replies)
Discussion started by: Anusha M
5 Replies

3. Shell Programming and Scripting

Passing arguments to interactive program through bash script, here document

Dear Users, I have installed a standalone program to do multiple sequence alignment which takes user parameters to run the program. I have multiple sequence files and want to automate this process through a bash script. I have tried to write a small bash code but its throwing errors. Kindly... (13 Replies)
Discussion started by: biochemist
13 Replies

4. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

5. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

6. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

7. Shell Programming and Scripting

[Bash] Beginner at scripting

Hi, I'm a beginner at shell scripting, just started scripting in bash a few days ago. I want to test if the command ls *.jpg returns exit code 2, and if yes I want to execute a new command ls *.jpeg, doing a test on it... and pretty much repeat the procedure. Is this correct? #!/bin/bash... (1 Reply)
Discussion started by: Utherr
1 Replies

8. Shell Programming and Scripting

Need help for scripting beginner

hy guys, I have perl script provided to me but i need to convert it into shell .Can you help me in this using sed shell command. cat /etc/passwd |perl -ne '/^(\w+):\w+: (\w+)/ and print "$1, $2\n";' (1 Reply)
Discussion started by: singh_king
1 Replies

9. Shell Programming and Scripting

Beginner bash scripting - a few problems

Hey Guys, I am creating a bash script on my freeBSD box, the script should basically ask the user to enter a username and domain. The script will take this information and basically append alot of information to config files so the user can receive email from that domain and create a web site at... (1 Reply)
Discussion started by: traxy
1 Replies
Login or Register to Ask a Question