How to compare a string value in an IF else condition using shell script

Viewed 17

#!/bin/sh

if [ $# -lt 2 ] then echo "Usage $0 servername envtype" exit 1 fi

servername=$1 envtype=$2 srv=echo $servername | tr "a-z" "A-Z" srvtype=echo $envtype | tr "a-z" "A-Z" echo $srv echo $srvtype

if [ "${srv}" <> "SCP" ]; then if [" ${srv}" <> "RENTD" ]; then echo "Invalid servername" exit 1; fi fi

I am getting this error when running the shell script

  • [ 2 -lt 2 ]
  • servername=scp
  • envtype=prd
  • echo scp
  • tr a-z A-Z
  • srv=SCP
  • echo prd
  • tr a-z A-Z
  • srvtype=PRD
  • echo SCP SCP
  • echo PRD PRD
  • [ SCP ]
  • [ SCP ] /tmp/testupper.sh: 19: [ SCP: not found
1 Answers

Found my issue.

[" ${srv}" <> "RENTD" ] 

there is a space being prefixed to the srv variable, between the " and the $: " ${srv}"

Related