LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
  Search this Thread
Old 11-03-2021, 08:53 PM   #1
bomberb17
Member
 
Registered: Jul 2005
Posts: 53

Rep: Reputation: 0
bash script and reading from file into variable


I have the following script named test.sh:

Code:
#!/bin/bash

flag=`cat ~/flag`
if [ "$flag" == "$1" ]; then
    echo OK
else
    echo FAIL
fi
Then I run the following:

Code:
$ echo 1 > ~/flag
$ ./test.sh
FAIL
Can someone help me understand why the first condition is not satisfied?
 
Old 11-03-2021, 09:11 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,898

Rep: Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837Reputation: 4837
The script looks like it worked as expected.

Without entering a command line argument when you ran it $1 is empty and therefore not equal to 1.
 
1 members found this post helpful.
Old 11-03-2021, 09:18 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,879
Blog Entries: 23

Rep: Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883Reputation: 3883
You have set the contents of the file flag to 1.

Your script reads the contents of that file into the variable $flag then your test compares the contents of that variable to the contents of the positional variable $1, which is empty at that point, so the test fails...

Try setting the positional variable and see what happens...

Code:
$ echo 1 > ~/flag
$ ./test.sh 1
The positional variables correspond to the command line args with $0 always being the command name, $1 being the first argument, $2 the second, etc.

^^^ michaelk types faster than I do - what he said!

Last edited by astrogeek; 11-03-2021 at 09:19 PM.
 
Old 11-03-2021, 09:24 PM   #4
bomberb17
Member
 
Registered: Jul 2005
Posts: 53

Original Poster
Rep: Reputation: 0
Thank you for your replies.
I actually don't want to have a command line argument.
My goal is to make a script that decides its conditional argument based on a variable fetched from a file on disk.
So if the file contents is "1" then output "OK", else output "FAIL".
How would I fix my script to make this happen?
 
Old 11-03-2021, 09:35 PM   #5
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 346

Rep: Reputation: 254Reputation: 254Reputation: 254
You would want something like this:

Quote:
#!/bin/bash

flag=`cat ~/flag`
if [ "$flag" -eq 1 ]; then
echo OK
else
echo FAIL
fi
 
1 members found this post helpful.
Old 11-03-2021, 09:49 PM   #6
bomberb17
Member
 
Registered: Jul 2005
Posts: 53

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Mechanikx View Post
You would want something like this:
Ah I see! So "-eq" instead of "==". Thanks!
 
Old 11-03-2021, 10:00 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,028

Rep: Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658Reputation: 2658
Only for num; for str it's still '=='
Code:
if [[ $flag == 'yourstringhere' ]]
In this case it appears you really do want the numeric, so this is just for your info ...
 
Old 11-04-2021, 01:24 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,406

Rep: Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234Reputation: 6234
Quote:
Originally Posted by bomberb17 View Post
Ah I see! So "-eq" instead of "==". Thanks!
not only use -eq for numbers, but use 1 on the right side, which is a number (or actually can be a string too), but $1 would be a variable ([almost] everything beginning with a $ is a variable) which was not initialized.

A comment: it is actually irrelevant if you compare 1 to 1 as numbers or as strings, the result will be exactly the same.
 
  


Reply

Tags
script


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Get bc script variable into a bash script variable? 14hei Programming 6 02-26-2016 07:34 AM
[SOLVED] Reading file into variable in bash ted_chou12 Linux - Newbie 3 03-21-2011 01:52 AM
Reading comma separated variable into other variables in shell script suryaemlinux Programming 5 12-22-2010 07:38 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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