If condition is -lt than 5 letters trouble.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition is -lt than 5 letters trouble.
# 1  
Old 02-08-2006
Error If condition is -lt than 5 letters trouble.

Hi,

In an If condition, how can I specify if the variable are less than 5 letters, echo “error”.

Something like this one:

echo –n “Your name please: “
read name
if [ $name –lt 5 #it must refer to 5 letters# ]; then
Echo “Your name must be at least 5 letters length”
exit
fi

Thanks a lot…
# 2  
Old 02-08-2006
Quick and dirty: if [ `echo $name|wc -c` -lt 5 ]; then
# 3  
Old 02-08-2006
Quote:
Originally Posted by RTM
Quick and dirty: if [ `echo $name|wc -c` -lt 5 ]; then
it doesnt work at well.

This is the code:

#!/bin/bash
clear
echo -n "Give me name: "
read let
if [ `echo $let|wc -c` -lt 5 ]; then
echo "There are not 5 letters"
else
echo "Thanks, there are 5 or more."
fi


here is the output:

Give me name: qwer
Thanks, there are 5 or more.


I got it....

The problem is on the space name: " it count that byte too...

okas...


Thanks a lot.
# 4  
Old 02-08-2006
Make it

if [ `echo $let|wc -c` -lt 6 ]; then


cheers
Gaurav
# 5  
Old 02-08-2006
Quote:
Originally Posted by TARFU
it doesnt work at well.

This is the code:

#!/bin/bash
clear
echo -n "Give me name: "
read let
if [ `echo $let|wc -c` -lt 5 ]; then
echo "There are not 5 letters"
else
echo "Thanks, there are 5 or more."
fi


here is the output:

Give me name: qwer
Thanks, there are 5 or more.


I got it....

The problem is on the space name: " it count that byte too...

okas...


Thanks a lot.
Try this.......

Quote:

#! /usr/bin/ksh
echo " Give me Name"
read name
LEN_NAME=`echo $name | awk '{ print length($0) }'`
if [ $LEN_NAME -lt 5 ]
then
echo "name less than 5"
else
echo "name greater than 5"
fi
# 6  
Old 02-08-2006
Just use # to get the length of a variable. It works in bash and ksh.
Code:
$ bash
$ x="abcd"
$ echo $x has ${#x} characters
abcd has 4 characters
$

# 7  
Old 02-08-2006
MySQL

Quote:
Originally Posted by mgirinath
Try this.......

Excellent, this one works.


Thanks a lot everyone....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Random letters

Hi there, first of all this is not homework...this is a new type of exercise for practicing vocabulary with my students. I have a file consisting of two columns, separated by a tab, each line consisting of a word and its definition, separated by a line break. What i need is to replace a... (15 Replies)
Discussion started by: eldeingles
15 Replies

2. 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

3. Shell Programming and Scripting

while condition with trouble

Hello! i have a llittle missunderstanding with a script and maybe you can explain where is the mistake. in the first place a have this script that is working just fine ... read -p "enter the name of the instance: " instance while ; .... but if i change the second condition with -z "`grep... (2 Replies)
Discussion started by: doro
2 Replies

4. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

5. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

6. UNIX for Advanced & Expert Users

Add letters

I want to add letters A,B,C,… in front of every line of input while printing them out using PERL. eg A file is parsed as a cmd line arg and its context will be displayed as A line1... B line 2.. I tried this..but I want better and perfect solution! !perl -p my $counter; BEGIN { $counter... (4 Replies)
Discussion started by: aadi_uni
4 Replies

7. Shell Programming and Scripting

Need to strip few letters

Hey guys.. Can experts help me in achieving my purpose.. I have a file which contains email address of some 100 to 1000 domains, I need only the domain names.. Eg: abc@yahoo.com hd@gamil.com ed@hotmail.com The output should contain only Yahoo.com ... (5 Replies)
Discussion started by: achararun
5 Replies

8. Shell Programming and Scripting

trim letters

Hello, I have a list of words.. ranging from 4 to any characters long.. to not more than 20 though. How can I select only first seven letters of the list of words? example:- wwwwwwwwww eeeee wererreetf sdsarddrereewtewt sdsdsds sdsd ereetetttt ewtwertwrttrttrtrtwtrww I... (10 Replies)
Discussion started by: fed.linuxgossip
10 Replies

9. Shell Programming and Scripting

transposing letters

Hi, I've written a shell function in bash that reads letters into an array, then outputs them in one column with: for n in "${array}"; do echo $n done I was wondering if anyone knew how i would transpose the letters that are output by the for loop. Right now my output is: aabbcc... (4 Replies)
Discussion started by: myscsa2004
4 Replies

10. UNIX for Dummies Questions & Answers

capital letters GONE!

I have an odd issue. I am trying to copy some files/folders to my linux box via a burned CD which I created on my mac. When I browse the files on the mac (or my windows box), everything looks fine (some of the folder names start with a capital letter, which is needed for everything to work... (8 Replies)
Discussion started by: blogg
8 Replies
Login or Register to Ask a Question