LinuxQuestions.org
Visit Jeremy's Blog.
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 09-14-2010, 03:53 PM   #1
unassassinable
Member
 
Registered: Sep 2010
Posts: 46

Rep: Reputation: 7
simple Bash script not returning correct value


I have a very simple bash script:
echo -n Create home directory?:
read HOMEDIR

if [ $homedir="y" ] || [ $homedir="yes" ]; then
homeval=" --makehomedir"
else
homeval="none"
fi

echo "the value is = " $homeval
Can someone please explain where my error is...why when echoing homeval (the last line of the script), the value is always --makehomedir, even when answering something other than y or yes?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-14-2010, 04:21 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
First, basically everything in Linux (and UNIX in general) is case-sensitive, so you've got to remember that HOMEDIR and homedir are different variables.

Second, without spaces in your comparison (e.g. if you do $homedir="y") that's actually assignment - you're assigning the value "y" to whatever the value of $homedir evaluates to be, and that will always be successful. You have to add spaces (make it $homedir = "y") for it to be treated as a comparison.
 
3 members found this post helpful.
Old 09-14-2010, 04:33 PM   #3
unassassinable
Member
 
Registered: Sep 2010
Posts: 46

Original Poster
Rep: Reputation: 7
Thank you!

Rich
 
Old 09-14-2010, 07:05 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
DOn't forget to mark as SOLVED once you have your solution
 
Old 09-15-2010, 12:24 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
It won't work if your directory name has spaces. Better put the variables inside quotes:
Code:
if [ "$homedir" = y ] || [ "$homedir" = yes ]; then
 
Old 09-15-2010, 02:15 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
It won't work if your directory name has spaces. Better put the variables inside quotes:
Not sure where you were going here Konsole, but the variable is storing y or yes not the directory name
 
Old 09-15-2010, 04:04 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by grail View Post
Not sure where you were going here Konsole, but the variable is storing y or yes not the directory name
Oh sorry. I was having too much thinking.. so.. anyway.. I think that will still cause error if he suddenly typed an IFS char.. But of course it can be ignored at preference.

..... I'm losing focus.. obviously. I've been doing many random things lately.. Things that are social related and project related.

Btw, I would always love to have my cybernick in a small letter K with the box attached.

---- edit ----

I just thought,.. maybe a short version would also be nice like konsole' or 'box. ^_^

Just kidding. As if the nick's really that special to have such an honor of being quoted.

Last edited by konsolebox; 09-15-2010 at 04:08 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
wcslen / _tcslen not returning correct value hs_linux Programming 4 02-16-2010 07:57 AM
Bash script, if else statement to ping & ssh connection to correct i.p. VipX1 Linux - Newbie 2 12-31-2009 11:05 PM
Nagios plugin check_snmp not returning correct information DrStrangepork Linux - Software 2 09-25-2009 10:43 AM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
Simple BASH script fio Linux - General 5 01-18-2003 03:26 PM

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

All times are GMT -5. The time now is 08:22 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