If condition fails for special charecter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition fails for special charecter
# 1  
Old 01-15-2019
If condition fails for special charecter

I have a sample server name listed in variable as below:

Code:
var="server-13"

I need to check if the 7th character on $var is number 1

Code:
 
 whichenv=`echo "$var"| head -c 7 | tail -c 1`

if [[ $whichenv -eq "9" ]]; then
echo "9 found"
 else
echo "9 Not Found"
fi

Output:
Quote:
[[: -: syntax error: operand expected (error token is "-")
This works fine but fails only if the seventh character is a hyphen "-".

Can you please suggest how can I overcome this issue ?
# 2  
Old 01-15-2019
Code:
[ "${var:7:1}" == 1 ]

must be enclosed in double quotes and compare strings.
Your code will work the same way.
# 3  
Old 01-15-2019
-eq does not exist in a double bracket expression, hence the syntax error. Either use (pattern matching)
Code:
if [[ $whichenv == 9 ]]

or (arithmetic comparison expression)
Code:
if (( whichenv == 9 ))

Note that whichenv does not contain the right value (this is not delivered by the head and tail utilities )
Try for example
Code:
whichenv=${var#*-}

To get the number behind the dash




--
Quote:
Originally Posted by nezabudka
Code:
[ "${var:7:1}" == 1 ]

must be enclosed in double quotes and compare strings.
Your code will work the same way.
Note: the OP was using a double bracket expression, where these quotes are not necessary..
With single brackets the correct expression is either (string comparison):
Code:
[ "$var" = 1 ]

or (numerical comparison)
Code:
[ "$var" -eq 1 ]


Last edited by Scrutinizer; 01-15-2019 at 05:14 PM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-16-2019
Quote:
Originally Posted by Scrutinizer
-eq does not exist in a double bracket expression, hence the syntax error. Either use (pattern matching)
Code:
if [[ $whichenv == 9 ]]

or (arithmetic comparison expression)
Code:
if (( whichenv == 9 ))

Note that whichenv does not contain the right value (this is not delivered by the head and tail utilities )
Try for example
Code:
whichenv=${var#*-}

To get the number behind the dash




--

Note: the OP was using a double bracket expression, where these quotes are not necessary..
With single brackets the correct expression is either (string comparison):
Code:
[ "$var" = 1 ]

or (numerical comparison)
Code:
[ "$var" -eq 1 ]

Thank you this works !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Retreive Files With a Special Condition?

Everyday I have to get a list of files in a directory with a special condition and feed this list to a for loop to be processed. Since I do not use Unix all the time, it is tricky for me to get that list of files. So, the question is whether there are commands that will give me the file names... (12 Replies)
Discussion started by: april
12 Replies

2. Shell Programming and Scripting

Need special charecter in email body

I have a unix shell script generate.sh that writes to a file hello.txt using redirect. For example:echo " Today's report shows progress by: " > hello.txt This hello.txt is then send as an email body to the recipients. My requirement is to have this special characters(up arrow and down arrow... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

How to exit from shell script if above condition fails?

HI cd ${back_home} if above back_home does not exist, then script shoul exit. Please let us know how to do that (7 Replies)
Discussion started by: buzzme
7 Replies

4. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

5. Shell Programming and Scripting

If condition matching with special chars

Hi, I have file #cat drivers.txt fcs0 fcs1 vscsi1 vscsi2 In this i need to check the availabality of "fcs" or "vscsi" alone not vscsi0,fcs1 I tried with "if condition" but it is not working. cat drivers.txt| while read ADAP do echo "Checking for $ADAP" if ;then echo "FC... (9 Replies)
Discussion started by: ksgnathan
9 Replies

6. Shell Programming and Scripting

Print every 5 lines with special condition

Hi Friends, I have an input file like this chr1 100 200 chr1 200 300 chr1 300 400 chr1 400 500 chr1 500 600 chr1 600 700 chr1 700 800 chr1 800 900 chr1 900 920 chr1 940 960 I would like to get the first line's second column and the fifth line's 3rd column as one single line. This... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

7. UNIX for Dummies Questions & Answers

If condition fails, advise using wildcard

OS Environment: HP-UX B.11.31 U ia64 I am using the shell script code to connect to Oracle RAC database. Passing the parameter of cluster database name. typeset -l DB_ID=$1 + typeset -l DB_ID=sivDB #---- 3. SetDB validation ------------- if ; then print... (3 Replies)
Discussion started by: Siva SQL
3 Replies

8. Shell Programming and Scripting

Stop execution of script if some condition fails

After my if condtion give rusult true then script should stop execution. Please advice...Thnaks in advance (1 Reply)
Discussion started by: vivek1489
1 Replies

9. UNIX for Dummies Questions & Answers

Strings with Special chars in IF condition

I was trying to run a code to check if a fax number is empty or not. for that, I've written the following code which is throwing an error. #!/bin/ksh fax= "999-999-9999" if ; then fax_no="000-000-0000" else fax_no=$fax fi echo $fax_no And I get the... (7 Replies)
Discussion started by: hooaamai
7 Replies

10. Shell Programming and Scripting

Comapring files charecter by charecter using AWK/Shell Script

Hi... I have a requrement to compare two files. for e.g. File 1 2007/08/19 09:48:10 DH-032 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-045 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-043 $APTA1: Device AATD8029 File 2 2007-08-19 09:48:10 DH-032... (1 Reply)
Discussion started by: evvander
1 Replies
Login or Register to Ask a Question