PDA

View Full Version : [ubuntu] "if...else" in Bash scripting



Xafer
May 25th, 2010, 02:03 AM
Sorry for asking questions simultaneously, I just curious with this Ubuntu :)
So, I would like to make a simple Bash code that read the users input.



#!/bin/bash
echo "Enter input"
read input
if [$inputan =1]
then
echo "You choose one"
else
echo "You choose other than one"
fi


So, when the users put 1, it will show a text "You choose one"
else, it will show "You choose other than one". The problem is whenever my input is, I always get this output

Enter input:
5
[: 9: missing ]
You choose other than one

swizman
May 25th, 2010, 02:52 AM
Change the if condition to:

if [ $input = 1 ]


You need empty space after and before the "[]" and also after the "=".
The variable containing the input is called $input, not $inputan.

Works fine with those changes.

BoneKracker
May 25th, 2010, 03:00 AM
http://www.ibm.com/developerworks/library/l-bash.html

http://linux.die.net/abs-guide/

Xafer
May 25th, 2010, 04:03 AM
Change the if condition to:

if [ $input = 1 ]


You need empty space after and before the "[]" and also after the "=".
The variable containing the input is called $input, not $inputan.

Works fine with those changes.
Thanks, great work, at the first i use
if[$input=1]
leaving all the spaces, and didnt work. but with spaces it works. thanks