Sponsored Content
Full Discussion: comparing strings as ints
Top Forums Shell Programming and Scripting comparing strings as ints Post 302692375 by Don Cragun on Monday 27th of August 2012 01:58:54 PM
Old 08-27-2012
The test utility's
Code:
s1 > s2

primary (which is an extension to the standards) performs a string comparison; the
Code:
s1 -gt s2

primary (which is required by the standards) performs a numeric comparison.

As an example:
Code:
if [ 5 > 10 ]
then echo string comparison
else echo numeric comparison

prints string comparison but:
Code:
if [ 5 -gt 10 ]
then echo string comparison
else echo numeric comparison

prints numeric comparison.

And, you got the syntax error because you need a space between the if and the [; i.e., if [ ... instead of if[ ...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

comparing two strings

Hi How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help if then echo success fi I am not getting the desired output if I do this. plz help (24 Replies)
Discussion started by: ragha81
24 Replies

2. UNIX for Advanced & Expert Users

Comparing strings

I have two strings a=Mar22 b=may21 how can I compare them Is this fine if then; . ... else .... fi or if then (2 Replies)
Discussion started by: yakyaj
2 Replies

3. Shell Programming and Scripting

Comparing Two Strings

Hi All, While I am trying to run below code I Am getting the exception like ./abs.sh: line 102: syntax error near unexpected token `then' ./abs.sh: line 102: ` then' The Code Snippet is: if then cat $file1 | sed -e... (8 Replies)
Discussion started by: Anji
8 Replies

4. Shell Programming and Scripting

comparing strings

i have a string in a file which gets repeated number of times like below: rpttxt("abc") . . rpttxt("REP_TITLE") rpttxt("BOS_TITLE") . . . . and so on using awk or grep how can i comapre the string( as the second half keeps varying) and store it in a temporary variable? I am using the... (3 Replies)
Discussion started by: agarwal
3 Replies

5. Shell Programming and Scripting

comparing 2 strings

hi i have 2 strings. i want to compare the strings. please help (2 Replies)
Discussion started by: satish@123
2 Replies

6. Shell Programming and Scripting

comparing two strings

hi All i am facing prob in comparing two strings that have two word. below is the code snippet. checkValidates="file validates" file3_name="file" if then echo "file" $file3_name "is validated successfully" fi when i run this i get the error as -bash: [: too many arguments ... (1 Reply)
Discussion started by: infyanurag
1 Replies

7. Shell Programming and Scripting

Comparing strings with sed

Input: The the the the Output: not-same same What would be the sed command to do this? (7 Replies)
Discussion started by: cola
7 Replies

8. Shell Programming and Scripting

Help with Comparing 2 strings from text

Hey guys how do I compare 2 strings from the text file, and check for duplication? For example, I add an item call Laptop, it will record to the textfile call file. If it detects duplicate it will say the record record exist? file.txt contains Laptop:Sony:1000 Phone:Apple:30 A head... (4 Replies)
Discussion started by: aLHaNz
4 Replies

9. UNIX for Dummies Questions & Answers

Strings comparing incorrectly

Hello I'm very new to Linux and shell scripting so I only know basic stuff. I'm making a script with the purpose of finding the longest string or word in a file. Here's what I got so far: #!/bin/bash longest="" for i in $(strings -n $1); do if ] then longest=$i fi done echo $longest... (4 Replies)
Discussion started by: SCB
4 Replies

10. Shell Programming and Scripting

Comparing Strings in ksh88

Hi I tried the following string comparison script in Ksh88 #!/bin/ksh str1='aC' str2='ABC' if then echo "Equal" else echo "Not Equal" fi Though str1 and str2 are not equal the script output says Equal . Please correct me Thanks (2 Replies)
Discussion started by: smile689
2 Replies
SUBSTR_COMPARE(3)							 1							 SUBSTR_COMPARE(3)

substr_compare - Binary safe comparison of two strings from an offset, up to length characters

SYNOPSIS
int substr_compare (string $main_str, string $str, int $offset, [int $length], [bool $case_insensitivity = false]) DESCRIPTION
substr_compare(3) compares $main_str from position $offset with $str up to $length characters. PARAMETERS
o $main_str - The main string being compared. o $str - The secondary string being compared. o $offset - The start position for the comparison. If negative, it starts counting from the end of the string. o $length - The length of the comparison. The default value is the largest of the length of the $str compared to the length of $main_str less the $offset. o $case_insensitivity - If $case_insensitivity is TRUE, comparison is case insensitive. RETURN VALUES
Returns < 0 if $main_str from position $offset is less than $str, > 0 if it is greater than $str, and 0 if they are equal. If $offset is equal to or greater than the length of $main_str, or the $length is set and is less than 1 (prior to PHP 5.6), substr_compare(3) prints a warning and returns FALSE. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.6.0 | | | | | | | $length may now be 0. | | | | | 5.1.0 | | | | | | | Added the possibility to use a negative $offset. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 A substr_compare(3) example <?php echo substr_compare("abcde", "bc", 1, 2); // 0 echo substr_compare("abcde", "de", -2, 2); // 0 echo substr_compare("abcde", "bcg", 1, 2); // 0 echo substr_compare("abcde", "BC", 1, 2, true); // 0 echo substr_compare("abcde", "bc", 1, 3); // 1 echo substr_compare("abcde", "cd", 1, 2); // -1 echo substr_compare("abcde", "abc", 5, 1); // warning ?> SEE ALSO
strncmp(3). PHP Documentation Group SUBSTR_COMPARE(3)
All times are GMT -4. The time now is 07:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy