Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Defunct Lizard

If Then Statement In Bash Not Working.

Recommended Posts

Posted · Original PosterOP

I am creating a bash script that installs programs and themes on Ubuntu Linux. Currently my script goes a little something like this:

GIMP=$" " gimp "Known as GIMP, GNU Image Manipulation Program is a free and open source photo editor." \

if [[ $GIMP == "gimp" ]]
then 
	sudo apt-get install $GIMP 
else
	echo "did not install gimp"
fi 

I am using ZENITY, which lets me open a text dialog box with columns, rows and check boxes.

The first part is my variable, where sets the variable GIMP as nothing or as "gimp". I have tested this and have confirmed that when I select the checkbox next to "gimp" it changes the value of the variable to "gimp"

The problem is that the if then statement is not working at all in the way it should, it always echos "did not install gimp".

 


Please don't argue with me, I am just trying to help, or be helped. (we are all humans right?)

 

 

 

 

Link to post
Share on other sites
56 minutes ago, Defunct Lizard said:

I have tested this and have confirmed that when I select the checkbox next to "gimp" it changes the value of the variable to "gimp"

How did you confirm this?


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
Posted · Original PosterOP
13 minutes ago, WereCatf said:

How did you confirm this?

i did "echo $GIMP"


Please don't argue with me, I am just trying to help, or be helped. (we are all humans right?)

 

 

 

 

Link to post
Share on other sites
Just now, Defunct Lizard said:

i did "echo $GIMP"

Just for sakes of completeness, try:

echo "\"$GIMP\""


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
Posted · Original PosterOP
13 minutes ago, WereCatf said:

Just for sakes of completeness, try:

echo "\"$GIMP\""

That also outputs "gimp".


Please don't argue with me, I am just trying to help, or be helped. (we are all humans right?)

 

 

 

 

Link to post
Share on other sites
Posted · Original PosterOP
44 minutes ago, WereCatf said:

How did you confirm this?

 

30 minutes ago, WereCatf said:

Just for sakes of completeness, try:

echo "\"$GIMP\""

My point in saying "I have tested this and have confirmed that when I select the checkbox next to "gimp" it changes the value of the variable to "gimp" " is just to indicate that the problem is the if then statement and not the variable. @WereCatf

 


Please don't argue with me, I am just trying to help, or be helped. (we are all humans right?)

 

 

 

 

Link to post
Share on other sites
1 minute ago, Defunct Lizard said:

My point in saying "I have tested this and have confirmed that when I select the checkbox next to "gimp" it changes the value of the variable to "gimp" " is just to indicate that the problem is the if then statement and not the variable. @WereCatf

Alas, there is nothing wrong with the if-statement.


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

If statement if good. But I'm wondering about hidden characters that don't appear when echo'ing.

 

Try like this?

if [[ $GIMP =~ "gimp" ]]
then 
	sudo apt-get install $GIMP 
else
	echo "did not install gimp"
fi 

EDIT: sorry wrong operation initially

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, WereCatf said:

Alas, there is nothing wrong with the if-statement.

Sorry, you are right. I checked again and:

GIMP=$" " gimp "Known as GIMP, GNU Image Manipulation Program is a free and open source photo editor." \

does not in fact work. I was confusing the automatic output of zenity and my own echo command. Is there something wrong with this ^. I've tried putting parenthesis around the code like this:

GIMP=$( " " gimp "Known as GIMP, GNU Image Manipulation Program is a free and open source photo editor." \ )

But is gives errors, whether I add a space in between it or not.

 

 

 

1 hour ago, DevBlox said:

If statement if good. But I'm wondering about hidden characters that don't appear when echo'ing.

 

Try like this?


if [[ $GIMP =~ "gimp" ]]
then 
	sudo apt-get install $GIMP 
else
	echo "did not install gimp"
fi 

EDIT: sorry wrong operation initially

This sadly also does not work. 🙁


Please don't argue with me, I am just trying to help, or be helped. (we are all humans right?)

 

 

 

 

Link to post
Share on other sites
6 hours ago, Defunct Lizard said:

Sorry, you are right. I checked again and:


GIMP=$" " gimp "Known as GIMP, GNU Image Manipulation Program is a free and open source photo editor." \

does not in fact work. I was confusing the automatic output of zenity and my own echo command. Is there something wrong with this ^. I've tried putting parenthesis around the code like this:


GIMP=$( " " gimp "Known as GIMP, GNU Image Manipulation Program is a free and open source photo editor." \ )

But is gives errors, whether I add a space in between it or not.

Well, yes, there is quite a lot of wrong with that and I, quite frankly, have no idea what you're trying to do there. I just thought you had some copy-and-paste error, because that line looks all mangled-up.


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
Posted · Original PosterOP
4 hours ago, WereCatf said:

Well, yes, there is quite a lot of wrong with that and I, quite frankly, have no idea what you're trying to do there. I just thought you had some copy-and-paste error, because that line looks all mangled-up.

https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

I'm trying to do a command substitution. The zenity code by itself works, what I'm trying to do is take the output of the code and set it as $GIMP.

@WereCatf


Please don't argue with me, I am just trying to help, or be helped. (we are all humans right?)

 

 

 

 

Link to post
Share on other sites
1 minute ago, Defunct Lizard said:

https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

I'm trying to do a command substitution. The zenity code by itself works, what I'm trying to do is take the output of the code and set it as $GIMP.

But you have no command there. If you wanted to e.g. use cat to read a text-file and put the file's contents into a variable, you'd do:

myVar=$(cat myfile.txt)


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×