Help with if loop (string comparison)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with if loop (string comparison)
# 1  
Old 07-04-2006
Help with if loop (string comparison)

Hi

Can someone please tell me what is wrong with this (ksh)..

Code:
	if [[ ${COMP_TEMP} = [0-9]+ ]] then
		echo ${COMP_TEMP}
	fi

What i need here is, say if the variable is a 1 or 2 digit number, then execute the if loop. Basically the variable can either be 1-30 or some other character sequence say '?', '&&' etc

Thanks.
# 2  
Old 07-04-2006
try this

x=`echo $COMP_TEMP | wc -c `
if [ $x -lt 4 ] then
echo $COMP_TEMP
fi
# 3  
Old 07-05-2006
Thanks, but that ain't going to work cause both 1 and ? are two bytes each.

The variable can either be a number from 1-30 or it can be some other character. I want the if loop to be a success only if the variable is a number from 1-30.
# 4  
Old 07-05-2006
Tried the below and it seems to work. Any idea why '+' doesn't work as intended? (1 or more)
Code:
if [[ ${COMP_TEMP} = [0-9]* ]] then

Thanks
# 5  
Old 07-07-2006
What ksh version are you using? The following works on ksh93 and recent versions of pdksh:
Code:
COMP_TEMP=12
[[ $COMP_TEMP == +([0-9]) ]] && print Match

[0-9] is the range, and ksh regex says "match one or more" with +(list)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

2. Shell Programming and Scripting

While loop for comparison

hi all, i have two files: test1 contains : one two three four five (on separate lines) test2 contains: one ten nine (on separate lines) I want to compare two files, test1 against test2. if any word from test1 does not match test2 display it. can someone help me with a while loop on this. ... (4 Replies)
Discussion started by: sbk785
4 Replies

3. Shell Programming and Scripting

Comparison of two variables with IF loop

Hello, My script is like : #!/bin/sh -x TODAY=$(echo `date +"%b%d"`) FILE_DATE=$(ls -l test.sh | awk '{print $6$7}') echo "file date=$FILE_DATE" echo "date=$TODAY" if ];then echo "file is correct" else echo "file is incorrect" fi and The Output is : $ ./test.sh + + date +%b%d +... (3 Replies)
Discussion started by: saurau
3 Replies

4. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help: while read line do # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output f_name=`echo $line | sed -e 's/^File://' -e 's/ *Status:.*//' | awk '{print $NF}'` ... (4 Replies)
Discussion started by: sudvishw
4 Replies

7. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies

8. Shell Programming and Scripting

Help with string comparison

#!/bin/sh PRINTF=/usr/bin/printf MACHINE_NAME=`uname -n` TIME=`date +"%H"` $PRINTF "Welcome to $MACHINE_NAME. What is your name?\n" read NAME if ; then $PRINTF "Good morning $NAME, how are you?\n" elif ; then $PRINTF "Good afternoon $NAME, how are you?\n" else $PRINTF "Good... (2 Replies)
Discussion started by: ikeQ
2 Replies

9. UNIX for Advanced & Expert Users

Comparison and For Loop Taking Too Long

I'd like to 1. Check and compare the 10,000 pnt files contains single record from the /$ROOTDIR/scp/inbox/string1 directory against 39 bad pnt files from the /$ROOTDIR/output/tma/pnt/bad/string1 directory based on the fam_id column value start at position 38 to 47 from the record below. Here is... (1 Reply)
Discussion started by: hanie123
1 Replies

10. Programming

String Comparison

Hi all, I have a file like this ibhib=ere wefwfl=werfe sfdes=wef From this file, i need to get the lefthand side string with respect to the corresponding righthand side string. i.e, I need to get the string "ere" with respect to "ibhib". But i am stuck with how to compare a string... (1 Reply)
Discussion started by: abey
1 Replies
Login or Register to Ask a Question