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 06-06-2012, 12:45 AM   #1
scandalist
Member
 
Registered: Apr 2011
Posts: 31

Rep: Reputation: 1
Script not working the way I programmed it to


I want to read user input and write to a file with bash but this is not working for some reason. The script executes but nothing happens upon my input.

Code:
#!/bin/bash

networking() {

echo "Press 1 to write to /etc/network/interfaces"

read choice

if [ choice = 1 ]



then

echo auto eth0 > /etc/network/interfaces
echo address 192.168.1.122 >> /etc/network/interfaces

fi

}

networking;
 
Old 06-06-2012, 12:51 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Code:
if [ "$choice" = "1" ]
Evo2.

PS. Others may also post and tell you to use [[ and ]] instead of [ and ] and also to use == instead of =. While that advice might be good practice for bash, it is not required to get your script working.
 
1 members found this post helpful.
Old 06-06-2012, 12:55 AM   #3
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,055

Rep: Reputation: Disabled
Bash works with strings I think and choice is a variable thus write:
Code:
 if [ $choice = "1" ]
Sorry, too late...

Last edited by Didier Spaier; 06-06-2012 at 12:57 AM.
 
Old 06-06-2012, 01:00 AM   #4
scandalist
Member
 
Registered: Apr 2011
Posts: 31

Original Poster
Rep: Reputation: 1
Wow, quick reply. Its doing what I want it to now, funny how every example I found online excluded that formatting. Thank you!
 
Old 06-06-2012, 01:08 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by evo2 View Post
PS. Others may also post and tell you to use [[ and ]] instead of [ and ] and also to use == instead of =. While that advice might be good practice for bash, it is not required to get your script working.
Also you do not need " around 1: [ "$choice" = 1 ] is sufficient.





_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 06-06-2012, 01:23 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by pan64 View Post
Also you do not need " around 1: [ "$choice" = 1 ] is sufficient.
True, but it's portable.

Evo2.
 
Old 06-06-2012, 01:31 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
also you might find it slicker to use the select statement in bash.
 
Old 06-06-2012, 11:46 AM   #8
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
In square-bracket shell tests "=,<,>," are string comparators. For integers, you need to use "-eq,-lt,-gt,-le,-ge".


Also, when using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression



And if I may offer another bit of advice, clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be so a year or so down the line).

Many people also feel that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block. Example:

Code:
for var in fee fai foo fum ; do

	if [[ "$var" == "foo" ]]; then
		echo "Found 'foo'."
	fi

done
 
  


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
What is linux programmed in... frostbute669 Programming 5 11-14-2005 02:40 PM
dlink 614+ -> Can it be programmed?!?! lazlow69 Linux - Hardware 0 03-22-2004 10:27 PM
Programmed Logout sinoob Programming 1 02-12-2004 07:02 AM
how was the first ever part of linux programmed? slakker Linux - General 1 05-01-2003 01:12 PM
Linux programmed with... chadb70 Linux - Newbie 2 03-11-2003 08:18 AM

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

All times are GMT -5. The time now is 08:02 AM.

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