Skip to main content
Solved

Bash script won't go into conditional statement even though it's true


Forum|alt.badge.img+5

Hi all. I've been working on a script that is used in self service which allows users to enter a tag number for a printer and then the printer is mapped. The script pulls from a CSV file. We have been able to make this work for all non-MFD machines and MFD machines separately. Unfortunately I cannot manage to get a combined script that will do both.
The reason this is occurring is that no matter what it always goes to the very last conditional statement, in most cases the else, even if the statement is true.

In these screen shots, you can see the script and what happens when I run (I excluded the prompt asking for the user input tag, but included showing the tag entered and showing that Canon is the manufacturer. I never receive the pop up, as I should since the if [ "$Manu" = "Canon" ] statement is true. I've tried many different combinations of syntax for this if/nested if/loop statement, and all results have it ignoring and going right to the final conditional. Any help would be great.

Best answer by jwenzel

I solved this myself fortunately. I made some changes to the conditionals, removing quotes and also using a wildcard to see if it was like "anon" essentially in place of Canon. I also added in the other statements below as a test so it works for both MFD and non MFD machines now.

View original
Did this topic help you find an answer to your question?

Tangentism
Forum|alt.badge.img+10
  • Honored Contributor
  • March 8, 2017

Try exporting the variables returned from the spreadsheet as they probably are not being passed to the Applescript


Forum|alt.badge.img+5
  • New Contributor
  • March 8, 2017

I solved this myself fortunately. I made some changes to the conditionals, removing quotes and also using a wildcard to see if it was like "anon" essentially in place of Canon. I also added in the other statements below as a test so it works for both MFD and non MFD machines now.


Tangentism
Forum|alt.badge.img+10
  • Honored Contributor
  • March 8, 2017

I've had this previously crossing into another language within the script, you have to export the variables.

#!/bin/sh
# Set start script runtime
startTime=$(date +%s)
sleep 30s
# Calculate script run time and display
finishTime=$(date +%s)
totalTime=$((finishTime - startTime))

export totalTime

# Applescript to display completed message
osascript -e 'display notification "Completed in '$totalTime' secs. with title "Script Timer"'

Forum|alt.badge.img+12

Hi guys -

Actually, a shell script or the shell will pass a bash variable to the osascript command without need for exporting. Also, in the osascript example above, there is a missing quote. Your command should look like this:

osascript -e 'display notification "Completed in '$totalTime' secs." with title "Script Timer"'

Your example is missing a double quote after the word "secs."

1 last thing: this is an odd example (as are most AppleScript snippets) of quoting. In most scripting languages single quotes enable literal strings, meaning, characters in between single quotes are interpreted as characters & special characters don't have their normal functions. In bash, this would mean that something like:

'$totalTime'

would not undergo variable expansion but would literally just print $totalTime. As you can see in AppleScript this is not the case, so, be careful. Happy Scripting!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings