mardi 7 mars 2017

Bash Script Always skips to bottom conditional even if first is true

I have a bash script that contains a small amount of Apple Script as well. This script is for printer management. We have some MFD printers that require mapping by IP at our organization, and then for all other printers we use a print server/queue name. If the script only includes a single conditional, mapping these MFD's with the method we're using works perfectly. For example:

INPUT=/Users/Shared/File.csv
OLDIFS=$IFS
IFS=","
while read Asset PQN IP Model Manu
do
    if [ "$Manu" == "Canon" ]; then
        $tagnum = $Asset
        sudo mv -n /Users/Shared/Drivers/* /Library/Printers/PPDs/Contents/Resources
        sudo mv -n /Users/Shared/Canon /Library/Printers/
        lpadmin -E -p "$PQN" -v "lpd://$IP" -P "/Users/Shared/PPD/$Asset.ppd" -o printer-is-shared=false
        cupsenable "$PQN"
        cupsaccept "$PQN"

        if [ $? -eq 0 ]; then
            osascript -e 'tell app "System Events" to display dialog "Printer '$PQN' Added!"'
        else
            osascript -e 'tell app "System Events" to display dialog "Error on Manu - The printer '$Asset' could not map correctly. Please call X at x." with text buttons {"OK"}'
        fi

    else
          do stuff here
    fi
done < $INPUT

If I were to remove the else statement here and just use the single if statement, it works no problem. However, if I add the else statement, it always, without fail goes to whatever the else statement is. This holds true if I turn it into an if, elif, else statement as well. I change the conditional not at all, but if it's anything but a solitary conditional statement then it always ignores the statement (even though it's true) and goes to the else or last argument.

Is it something to do with being in the while loop? I can't wrap my head around the logic. Ideally once we figure out how to make it so it doesn't always go to the last conditional option, we can add another argument so that we can map the non-MFD printers using print server/queue name, which we have working fine as well in another script. Currently that's not possible as I can't figure out how to do more than one argument without this issue occurring.

Aucun commentaire:

Enregistrer un commentaire