Skip to content

Commit 00afc99

Browse files
authored
Update file existence checks in goostats.sh
The `-a` option for `if` does not work in all environments. I have tested the script below in three different environments, Git Bash, MobaXTerm and RedHat Linux and they all produce the following output: # The Script ``` #!/bin/bash touch checkForfile.md echo WITH -a if [ -a checkForfile.md ]; then echo yes; else echo no;fi if [ -a NocheckForfile.md ]; then echo yes; else echo no;fi echo WITH ! -a if [ ! -a checkForfile.md ]; then echo no; else echo yes;fi if [ ! -a NocheckForfile.md ]; then echo no; else echo yes;fi echo WITH -e if [ -e checkForfile.md ]; then echo yes; else echo no;fi if [ -e NocheckForfile.md ]; then echo yes; else echo no;fi echo WITH ! -e if [ ! -e checkForfile.md ]; then echo no; else echo yes;fi if [ ! -e NocheckForfile.md ]; then echo no; else echo yes;fi echo WITH -f if [ -f checkForfile.md ]; then echo yes; else echo no;fi if [ -f NocheckForfile.md ]; then echo yes; else echo no;fi echo WITH ! -f if [ ! -f checkForfile.md ]; then echo no; else echo yes;fi if [ ! -f NocheckForfile.md ]; then echo no; else echo yes;fi ``` # The output ``` $ ./checkforfile.sh WITH -a yes no WITH ! -a no no WITH -e yes no WITH ! -e yes no WITH -f yes no WITH ! -f yes no ``` # Expected output The expected output for all tests would be: ``` yes no ``` However, the `-a` option does not work if used in conjunction with not ('!')
1 parent 4f0a496 commit 00afc99

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

shell-lesson-data/north-pacific-gyre/goostats.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ then
1111
fi
1212

1313
# check if files already exist (good for $1, bad for $2)
14-
if [ ! -a "$1" ]
14+
if [ ! -f "$1" ]
1515
then
1616
echo "error reading input: $1"
1717
exit 2
18-
elif [ -a "$2" ]
18+
elif [ -f "$2" ]
1919
then
2020
echo "error writing result: $2"
2121
exit 2

0 commit comments

Comments
 (0)