Jithin Kumar S
Jithin Kumar S

Reputation: 873

Shell Script compare the values with input parameter

apps="http:git.abc.com";
cluster-ui="http:git.xyz.com";
customer-ui="http:git.xxx.com";
SERVICE=$1;

My requirement is if I pass service name as a 'apps' then I need to clone the $apps url. Here

if [ $Service -eq apps ] 

not think a good approach as my repo url might get increased so more and more loop will come Any suggestions?

Upvotes: 0

Views: 3836

Answers (2)

Jithin Kumar S
Jithin Kumar S

Reputation: 873

'Case statement' would suit here more than if ladder

Upvotes: 0

Ericgit
Ericgit

Reputation: 7063

The $ sign assigns the input argument, so we're getting first input if it matches the below variable, so do what you want inside if condition.

#!/bin/bash
apps="http:git.abc.com";
clusterui="http:git.xyz.com";
customerui="http:git.xxx.com";
#SERVICE=$1;

#Store global
repo=''

# if empty parameter is passed
if [ $# -lt 1 ] ; then
   echo "Parameters Need"
exit 1
fi;

# for search the correct parameter

if [ $1 = "apps" ]; then
   repo=$apps
elif [ $1 = "cluster-ui" ] ; then
  repo=$clusterui
elif [ $1 = "customer-ui" ] ; then
  repo=$customerui
else
   echo "Not found"
fi;

echo $repo

Note just repeat elif [ ] ;then for more entries or think!

how to access run this file like this sh ./file.sh apps just replace apps with yours. make sure you have permission to execute the file if you don't have, give it to permission like below

chmod 766 file

now run the shell script sh ./file.sh clusterui

Upvotes: 1

Related Questions