How does a if works in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How does a if works in shell
# 1  
Old 11-24-2011
How does a if works in shell

Hi,

I need to compare and get an output for values greater than "X10" (values contain both characters and numbers) using if loop...

FOR EG:

I want to export values greater than X10, i.e., in-case if the value is X11 and greater than the "if" part to be executed if the value is X9 and less than that "else" part to be executed.

I tried the below code, when i passes A as X9 or X11 also the "ELSE" part is only executed
Code:
if [ $A -gt X10 ];then
    export "$A is correct"
else
    export "$A is incorrect"
fi

Can any one correct me where i went wrong???

Thanks
Shiva

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-24-2011 at 03:33 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-24-2011
Hi,

when comparing string (sorting) order replace the if..else conditional statement with:

Code:
[[ "$A" > "X10" ]] && echo "$A is correct" || echo "$A is incorrect"

Which basically means:
if the test inside the double square brackets returns true, display "$A is correct" else display "$A is incorrect".

You can find more about [[ ]] tests in bash man page, section 'Compound Commands'.

Keep in mind that you are comparing strings, so "X9" is actually > (that is, greater than) "X10".

see ya
fra

Last edited by frappa; 11-24-2011 at 04:07 AM.. Reason: added further explanation
# 3  
Old 11-24-2011
deleted the content ..

Last edited by jayan_jay; 11-24-2011 at 04:07 AM.. Reason: saw the update
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to understand how the line in perl program works as shell commend

I have a file with two line, one is header, the other actual value: TYPCD|ETID2|ETID|LEG ID|PTYP|PTYP SUB|TRD STATUS|CXL REASON|CACT|CACTNM|ENCD|ENC D NM|TRDR|ASDT|TRDT|MTDT|STDT|LS|SECID|SECID TYP|SECNM|PAR|STCC|MARKET PRICE|DIS MARKET PRICE|MARKET PRICE CURRENCY|SRC OF SETTLEMENT... (2 Replies)
Discussion started by: digioleg54
2 Replies

2. Shell Programming and Scripting

SUID works for shell scripts??

SUID works for shell scripts (bash)?? I'm user user1 need to execute a shell script (script.sh) which is owned by user2.-rwsrwxrwx 1 user2 aduser 3518 Aug 21 05:33 script.shOnly user2 has write privileges to write/copy a file in directory /dir1/subdir. Hence SUID bit permissions are set to... (8 Replies)
Discussion started by: MSK_1990
8 Replies

3. Shell Programming and Scripting

I am learning shell scripting and i would like to understand how sort -k3,3 -k 4,4 short* works

Please help me understand how sort -k3,3 -k 4,4 short* works , is it sorting 3 and 4 th field ? what is the use of specifying 3,3 and 4,4 in sort and what is actually does and i see sort -k3 short* and sort -k3,3 -k 4,4 short* giving the same output. Sort output attached Please help (2 Replies)
Discussion started by: Antony Ankrose
2 Replies

4. Shell Programming and Scripting

perl: Command works in terminal, but not in shell script

Hi, the following command works in the terminal no problem. samtools view -h rawlib.bam | perl -ne '{ @line = split( /\s+/ ); $match = 0; while( $line =~ /(\d+)M/g ) { $match = $match + $1 } if( $match >= 80 || $_ =~ /^\@/ ) { print $_ } }' | java -Xmx12G -jar... (8 Replies)
Discussion started by: jdilts
8 Replies

5. Shell Programming and Scripting

variable assigment not works in shell script

Hi, The following assigment is not working within shell script but is working from command line. Could anybody advise why? OS - solaris 8 APPL=`grep "$Application" ldapapps |awk '{print $1}'` echo $APPL (5 Replies)
Discussion started by: urello
5 Replies

6. UNIX and Linux Applications

Rsync works in shell but not in cron

So I have this rsync script I wrote to grab some sql files and import them to a database. I left in the mysql stuff just give context to the situation. The real problem is with my rsync code. script.sh (chmod 744) #!/bin/sh rsync -av --rsh="sshpass -p'PASSWORD' ssh -l'USERNAME'"... (3 Replies)
Discussion started by: noPermissions
3 Replies

7. Shell Programming and Scripting

how the typeset command works in shell script

typeset -l section section=${2:-.} what does these 2 lines meaning? (1 Reply)
Discussion started by: venkatababu
1 Replies

8. Shell Programming and Scripting

Works in shell but not script UNIX

ok i have a very simple UNIX script #!/bin/bash TERM=ansi;export TERM PFCMARK=25;export PFCMARK umask 0000 PFUMASK=000;export PFUMASK #run for filepro menus and exectuables echo "###########File Modification Log.############\r" > "/public/appl-fp$(date +%m-%d-%Y).txt" find /appl/fp/... (10 Replies)
Discussion started by: dunpealslyr
10 Replies

9. Shell Programming and Scripting

Execution issue with shell script - works in a different environment

Hi I get the following error while executing the shell script. I did not get an error when I ran the script in a different environment (unix server). str-token.ksh: 0403-057 Syntax error at line 20 : `(' is not expected. This is the line which gives error string=(${pos_array}) Please find... (3 Replies)
Discussion started by: hidnana
3 Replies

10. Shell Programming and Scripting

substring command works but only in BASH shell

I am having trouble running a .sh file. The code 'x=${file_name:0:$z-11}' is giving me a bad substitution error. However when I run in BASH it works. Thing is when this goes to production the .sh will not be running in BASH. Is there a way to substring a string not in BASH or a way to invoke... (2 Replies)
Discussion started by: edwardtk11
2 Replies
Login or Register to Ask a Question