Reputation: 117
When I echo out the file and when I put the name in the command directly, it works fine, but when I run it with the file name as a variable. It gives me a BAD vs a GOOD. this is obviously a shorter equation to the longer one I am trying ot get to work, but I feel if I can get this to work, then I am good.
Anyone? Any Ideas why it gives me a BAD for this?
if [ -f /home/publish/swift/$TAR_FILE ]; then echo "good"; else echo "bad"; fi
But a GOOD when I use:
if [ -f /home/publish/swift/swift_tar_1212.tgz ]; then echo "good"; else echo "bad"; fi
Upvotes: 0
Views: 84
Reputation: 117
This problem eluded me for a few days and I knew something was weird with the string return form the other server. I have added the code that ended up working for me.
GOAL: ssh into other server, run a command in a certain directory to determine the most recent updated .tgz, then take the name of that file and pass it into a variable so that it could be run from the current instance to SCP over. Code is below
#SSH into the master prod machine and find the latest TAR.
TARFILE="$(ssh -i cross-key -t login@server 'bash -c "cd /home/publish/swift; find *.tgz -type f | xargs ls -t"')"
#Telling us the latest .tgz of Swift Website
echo "TAR List:$TARFILE"
#Pass the List into an Array and then Get the first one which is the latest .tgz file
IFS=' ' read -a array <<< "$TARFILE"
TAR_FILE "${array[0]}"
This finally gave me the string that I needed to continue with my if and else statement.
if [ -f /home/publish/swift/$TAR_FILE ]; then echo "good"; else echo "bad"; fi
And I was able to get good - therefore I could continue with the execution of the new tgz file. I had to pass the full list into an array vs just taking the last value with:
$(find *.tgz -type f | xargs ls | tail -n 1)
What a freakin nightmare. But now it works! I just hope this can help someone else that beats their head against the wall about this problem.
Upvotes: 0
Reputation: 428
If you set the TAR_FILE to the same value, then it should work
TAR_FILE='swift_tar_1212.tgz'
And please do yourself a favor and ALWAYS put the string into quotes:
if [ -f "/home/publish/swift/$TAR_FILE" ]; then echo "good"; else echo "bad"; fi
Single quotes, if you do not want an $... expansion.
Maybe you will find it useful to set xtrace:
set -x
turning it off with
set +x
If you want to glob for all files in a dir, without knowing their name:
for file in /home/publish/swift/*; do
test -f "$file" && echo "$file"
done
All files is not perfectly correct, the hidden files are not matched this way!
for file in /home/publish/swift/{.*,*}; do
test -f "$file" && echo "$file"
done
As @mklement0 noted, you may use shopt to modify the globbing => http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html In your case the following options may help you:
dotglob
extglob
failglob
globstar
nocaseglob
nocasematch (``[[ ]]`` only)
nullglob (very useful)
It can be confusing to set these values in your .bashrc, because a lot of examples in the web are based upon the default values. Unfortunately shopt cannot be restricted to a shell function - thats pain.
Upvotes: 3
Reputation: 4837
Try like this
if [ -f "/home/publish/swift/$TAR_FILE" ]
then
echo "good"
else
echo "bad"
fi
Upvotes: 1