LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2012, 12:04 AM   #1
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Rep: Reputation: 0
Bash variable length issues


So I'm having an issue with checking the length of a variable in an if statement like so:

Code:
variable="sometext"

if [[ "${#variable}" > 16 ]]; then
     echo "Fail"

else
     echo "Success"

fi
why doesn't this work? it's smaller so it should resort to else and echo Success but it echo's Fail to the screen. I'm so lost, I've even tried using quotes and taking quotes from areas and it changes nothing as far as I could see. ${#variable} does give you the length of a variables contents...right? If I'm doing this wrong which I do not see how I could be with all the examples I just tested. Thanks in advance

Should I maybe just use awk or expr. Rather not use expr because from what I found out mac doesn't have that built into there OS and I wish to allow for my girlfriend to use this script and she isn't very tech savy and I can't ssh into her machine to install things myself but expr I believe if I remember right a bash function but she is all the way back in bash 3.2 or 3.1

Fixed: I found what I was doing wrong I forgot when using numbers you need (( )) so thats how you fix it :P

Last edited by fatalerror0x00; 12-17-2012 at 12:42 AM.
 
Old 12-17-2012, 02:37 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Try this:
if [[ ${#variable} -gt 16 ]]; then
 
Old 12-19-2012, 08:10 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As gnashley demonstrated, in shell tests ">" and "<" and the like are string comparisons. They evaluate text strings according to their ascii values, one character column at a time (AIUI). Since "8" is greater than "1", the test comes out true.

For numeric comparisons in square-bracket style tests, use -gt/-lt/-ge/-le/-eq instead. But as you discovered, if you're using bash or ksh or the like, you should use ((..)) arithmetic evaluation brackets instead. Then you can use the arithmetic symbols as intended.

Code:
variable="sometext"

if (( "${#variable}" > 16 )); then
     echo "Fail"
else
     echo "Success"
fi
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://mywiki.wooledge.org/ArithmeticExpression

Last edited by David the H.; 12-19-2012 at 08:11 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New Year's gift for you: A variable-length bash prompt David the H. Linux - General 9 03-21-2014 06:04 AM
[SOLVED] Convert length-indicated variable length record file to LF-terminated Z038 Linux - General 10 11-29-2012 11:59 PM
Extracting nth length of character from a variable to another variable hayloiuy Linux - Newbie 4 05-02-2011 03:47 PM
Bash read in variable length text records lynx81191 Programming 4 11-17-2007 08:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration