LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 01-17-2008, 03:05 AM   #1
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
Cool read command in shell script what to do for reading a particular char


What is the shell command like read to read a particular char from input
like if i want c to be entered to go forward and execute another shell commands.
 
Old 01-17-2008, 03:21 AM   #2
sophkos
LQ Newbie
 
Registered: Jan 2008
Distribution: Fedora 7
Posts: 16

Rep: Reputation: 0
If you want c to be entered you type: read c
 
Old 01-17-2008, 03:34 AM   #3
amitsurana
LQ Newbie
 
Registered: Oct 2007
Location: India
Distribution: ubuntu, redhat
Posts: 5
Blog Entries: 1

Rep: Reputation: 0
read Value
if(($value=="c"))
then
echo "move forward"
fi



m not sure of scripting syntax here
but using "read <variable-name>" will read the standard input into <variable-name>.
 
Old 01-17-2008, 04:43 AM   #4
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Original Poster
Rep: Reputation: 15
Unhappy

Quote:
Originally Posted by amitsurana View Post
read Value
if(($value=="c"))
then
echo "move forward"
fi



m not sure of scripting syntax here
but using "read <variable-name>" will read the standard input into <variable-name>.


I have written the code as


date
read value1
if [ $value1="acd" ]
then
who
fi
exit

now it should print only the date and exit but instead it is printing date and then executing who command.
so what should be the testing conditions in if [..] to compare with value1
and then decide whether to execute who or not.
 
Old 01-17-2008, 11:38 PM   #5
amitsurana
LQ Newbie
 
Registered: Oct 2007
Location: India
Distribution: ubuntu, redhat
Posts: 5
Blog Entries: 1

Rep: Reputation: 0
Quote:
Originally Posted by Ashok_mittal View Post
I have written the code as


date
read value1
if [ $value1="acd" ]
then
who
fi
exit

now it should print only the date and exit but instead it is printing date and then executing who command.
so what should be the testing conditions in if [..] to compare with value1
and then decide whether to execute who or not.

the IF statement synatax is :
if[$value -eq <your-string>]
then
<statements>
fi
<more - statements>
 
Old 01-18-2008, 12:38 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,093

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Actully, it's

Code:
if [[ $val = "test_str" ]]
then
    <do stuff>
else
    <do other>
fi
note the spaces around the double '[[' ']]' and also note that you use '=', '<' etc for string comparisons and -eq etc for numeric comparisons: http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 01-18-2008, 12:46 AM   #7
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Original Poster
Rep: Reputation: 15
Unhappy

Quote:
Originally Posted by chrism01 View Post
Actully, it's

Code:
if [[ $val = "test_str" ]]
then
    <do stuff>
else
    <do other>
fi
note the spaces around the double '[[' ']]' and also note that you use '=', '<' etc for string comparisons and -eq etc for numeric comparisons: http://www.tldp.org/LDP/abs/html/comparison-ops.html

it is still executing 'who' command in if condition no matter the condition is true or false.
 
Old 01-18-2008, 05:48 AM   #8
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
this script works perfectly fine on my computer:
Code:
#!/bin/bash

read value
if [[ "$value" == "anda" ]]; then
	echo "You entered anda"
else
	echo "You didn't entered anda, instead you entred $value"
fi
 
Old 01-21-2008, 02:40 AM   #9
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Original Poster
Rep: Reputation: 15
Cool

Quote:
Originally Posted by anupamsr View Post
this script works perfectly fine on my computer:
Code:
#!/bin/bash

read value
if [[ "$value" == "anda" ]]; then
	echo "You entered anda"
else
	echo "You didn't entered anda, instead you entred $value"
fi

Thanks for your valuable help but it won't work unless there are spaces b/w
"$value" and '==' & '==' and "anda"
for the expected comparison "$value"="anda" the condition must be like this.
 
Old 01-21-2008, 03:29 AM   #10
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Of course it won't work if you don't have spaces between them. That is why there are spaces between them. I don't understand what you mean.
 
Old 01-21-2008, 03:39 AM   #11
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Original Poster
Rep: Reputation: 15
Unhappy

Quote:
Originally Posted by anupamsr View Post
Of course it won't work if you don't have spaces between them. That is why there are spaces between them. I don't understand what you mean.
Yes you are absolutely right but can u get me understood why the gcc compiler making the condition always true when there are no spaces whether the strings are same or different . Like

read value1
echo "value is $value1"
if [[ "$value1"=="acd" ]];
then
pwd
else
who
fi



It is executing command 'pwd' in any case.
 
Old 01-21-2008, 05:01 AM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,375

Rep: Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911
If you don't put spaces around the = or == signs, the shell will not interpret it as a comparison:
Code:
[[ string == string ]]
but as a single random string:
Code:
[[ string ]]
and a random string always evaluates as TRUE. See The Advanced Bash Scripting Guide, Section 7.1 for more details.
 
  


Reply

Tags
make, people


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SHell Script Help! When reading input from a user sfmadmax Linux - Newbie 3 12-12-2007 12:03 AM
shell script for reading of configuration files nhydra Programming 2 06-13-2006 03:15 AM
reading idle time with perl/shell script daryl314 Linux - General 1 12-27-2004 01:11 PM
Shell Script to read 500files from the command line saravanan1979 Programming 1 09-22-2004 09:44 AM
Shell Script: Reading Pdf files funkymunky Programming 8 06-18-2004 03:34 PM


All times are GMT -5. The time now is 03:01 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration