[SOLVED]How to compare two string variables - Bash Scripting

This forum is dedicated to basic help and support :

Ask here your questions about basic installation and usage of Mageia. For example you may post here all your questions about getting Mageia isos and installing it, configuring your printer, using your word processor etc.

Try to ask your questions in the right sub-forum with as much details as you can gather. the more precise the question will be, the more likely you are to get a useful answer

[SOLVED]How to compare two string variables - Bash Scripting

Postby bots4ever » Sep 29th, '14, 12:04

Greetings fellow Mageia users!

I am trying to customize my conky script and I ran into some challenges (technical things that are way over my head at the moment). Basically what I wanted to do is to create an exec command for my conky to run this script that I am working on.

The thing is, my laptop has 2 heads - an Intel HD4000 and an ATI Radeon 7670M card. I would like my conky to display the temperature of my Radeon card if it is selected and display something like "disabled" or something if I decide to use my Intel HD4000 graphics. Here is the script:

Code: Select all
value1="ERROR - X needs to be running to perform AMD Overdrive(TM) commands"
command1=$(aticonfig -q --od-gettemperature > output)
value2=$(cat output)
if [$value2 -eq $value1]
  then
    echo $value2
  else
    $command1
fi


The script is really basic but to give you an overview...
"value1" is a static variable text
"command1" is the command to extract the information, particularly the temperature, of my ATI Card and outputs the details to a textfile in the same directory where the shell script is saved (later i'll just use grep to drill down the exact data I wanted to extract).
"value2" to get the value of the information contained in "output" file from the previous command.

I used an IF THEN ELSE statement for this one and I am always getting an error
line 4: [ERROR: command not found
I substituted "-eq" with "=="; played a lot with namespaces [ if (argument 1) (argument 2) ] vs [if (argument 1) (argument 2)] and many other spacing combinations; I also tried [ if (argument1) (argument2) then ] instead of placing "then" as listed on the code

By the way, if I run aticonfig command when I'm using my Intel HD4000, I get the this message
ERROR - X needs to be running to perform AMD Overdrive(TM) commands
.

My bash version is
GNU bash, version 4.2.49(2)-release (x86_64-mageia-linux-gnu)
by running
Code: Select all
bash --version


And lastly, I am using Mageia 4 64bit dualbooting with CentOS 6.5 64bit if that matters.

Please ask questions if there is something I missed.
Last edited by bots4ever on Oct 1st, '14, 15:11, edited 1 time in total.
bots4ever
 
Posts: 35
Joined: May 4th, '14, 03:13
Location: Philippines

Re: Need help with a script

Postby ITA84 » Sep 29th, '14, 15:50

The correct syntax to compare strings would be

Code: Select all
if [ "$value1" = "$value2" ]
then
    ...
else
    ...
fi


By the way, just using $command1 in the else branch would probably accomplish nothing, as $command1 would be replaced by the output of aticonfig... (which might be empty since you redirect stdout to file).
ITA84
 
Posts: 199
Joined: Mar 5th, '13, 18:15

Re: Need help with a script

Postby doktor5000 » Sep 29th, '14, 18:09

I can only recommend you to read some of the examples in http://mywiki.wooledge.org/BashPitfalls.

Three recommendations:
- -eq, as any other of the logical operators can only compare integers, not strings, for strings you can only use = or != [to put it simple]
- don't use simple test command "[" but use the shell builtin "[[" instead - always!
- for such simple two-case comparisons don't use if .. then .. else but only use the above mentioned super-test, hence for your case I'd use
Code: Select all
[[ "$value1" = "$value2" ]] && { echo $value2 ; } || { $command1 ; }



Not looked closer at your script, but instead of that you should even be able to simplify it using something like this:
Code: Select all
FOO="${BAR:-new}"
means that use $BAR if nonzero, otherwise set it to "new"
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18064
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: Need help with a script

Postby bots4ever » Oct 1st, '14, 15:09

Sorry it took me a while to respond back. I got into more noob problems than I originally thought i'd encounter with my Conky project.

Now, my script works as intended and for your reference, here it is.

Code: Select all
sample="aticonfig: No supported adapters detected"
aticonfig --adapter=0 --od-gettemperature > temp 2>&1
output=$(<temp)
set -- $output
if [ "$output" == "$sample" ];
then echo "Disabled";
else aticonfig --adapter=0 --od-gettemperature | grep -e "Sensor 0" | cut -b 37-43;
fi


Thank you ITA84 for pointing that out. I followed your suggestion.

Thank you doktor5000. I will certainly take a look at the link you provided once I started getting serious with my shell scripting lessons. :)

With this I am marking this thread closed and renaming into a more appropriate topic so other newbie users can benefit. Thanks again!
bots4ever
 
Posts: 35
Joined: May 4th, '14, 03:13
Location: Philippines


Return to Basic support

Who is online

Users browsing this forum: No registered users and 1 guest