1

Running the below returns for lines of echoed text, but only the first app from the array actually gets touched:

#!/bin/sh

path=/Applications

app[0]="Microsoft Communicator.app"
app[1]="Microsoft Lync.app"
app[2]="Microsoft Messenger.app"
app[3]="Remote Desktop Connection"

IFS=""

for i in ${app[*]}
do
        if [[ -a $path/$app ]];
        then
            chflags hidden $path/$app;
            echo "Hiding $app"
        fi;
done

exit

2 Answers 2

2

In addition to the basic problem Artur R. Czechowski pointed out, you really should be double-quoting all variable references (and using [@] instead of [*]). This is the proper way to handle variables with spaces; setting IFS will sort of work, but can break other things. Also, you don't need semicolons at the end of lines in bash. Here's my suggested rewrite:

for i in "${app[@]}"
do
    if [[ -a "$path/$i" ]]  # Double-quotes not strictly needed here, but I consider it a good habit
    then
        chflags hidden "$path/$i"
        echo "Hiding $i"
    fi
done
Sign up to request clarification or add additional context in comments.

Comments

1

In the loop use $i instead of $app, like below:

for i in ${app[*]}
do
        if [[ -a $path/$i ]];
        then
            chflags hidden $path/$i;
            echo "Hiding $i"
        fi;
done

2 Comments

That did it - thanks. Trying to learn the 'real' way to script.
@da4 In that case you may find ShellCheck useful. When running your script through it, it said "i appears unused. Verify it or export it."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.