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

jwenzel
New Contributor II

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.1e1b59bb90cb4df48420fbafb91ef250
96d8671f3b5740a8a82b9553ba6316b1

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.

1 ACCEPTED SOLUTION

jwenzel
New Contributor II

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.

b8436749a0bb406385eaad03b3084f77

View solution in original post

5 REPLIES 5

jwenzel
New Contributor II
 

Tangentism
Contributor III

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

jwenzel
New Contributor II

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.

b8436749a0bb406385eaad03b3084f77

Tangentism
Contributor III

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"'

brock_walters
Contributor
Contributor

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!