bash script and reading from file into variable
I have the following script named test.sh:
Code:
#!/bin/bash Code:
$ echo 1 > ~/flag |
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. |
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 ^^^ michaelk types faster than I do - what he said! |
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? |
You would want something like this:
Quote:
|
Quote:
|
Only for num; for str it's still '=='
Code:
if [[ $flag == 'yourstringhere' ]] |
Quote:
A comment: it is actually irrelevant if you compare 1 to 1 as numbers or as strings, the result will be exactly the same. |
All times are GMT -5. The time now is 01:43 AM. |