Empty string is a file? ( if [ ! -f ââ ] )
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
The script is called isFile.sh and looks like this:
#!/bin/sh
echo $1
echo $2
if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi
if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi
First I created a file by doing touch file.exist
.
And I ran bash isFile.sh file.exist file.notexist
The output was:
file.exist
file.notexist
file.notexist (arg2) is not a file
Then I ran bash isFile.sh "" file.notexist
The output was:
(# empty line)
file.notexist
file.notexist (arg2) is not a file
Expected output is:
(# empty line)
file.notexist
(arg1) is not a file
file.notexist (arg2) is not a file
Can somebody explain why?
files string test
add a comment |Â
up vote
-1
down vote
favorite
The script is called isFile.sh and looks like this:
#!/bin/sh
echo $1
echo $2
if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi
if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi
First I created a file by doing touch file.exist
.
And I ran bash isFile.sh file.exist file.notexist
The output was:
file.exist
file.notexist
file.notexist (arg2) is not a file
Then I ran bash isFile.sh "" file.notexist
The output was:
(# empty line)
file.notexist
file.notexist (arg2) is not a file
Expected output is:
(# empty line)
file.notexist
(arg1) is not a file
file.notexist (arg2) is not a file
Can somebody explain why?
files string test
Are you sure aboutecho$1
? I would think it isecho $1
(better cut an paste). And welcome!
â Volker Siegel
Aug 7 at 6:01
@ Volker Siegel, Sure it'secho $1
, I edited my question, thanks.
â SoloKyo
Aug 7 at 6:04
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
The script is called isFile.sh and looks like this:
#!/bin/sh
echo $1
echo $2
if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi
if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi
First I created a file by doing touch file.exist
.
And I ran bash isFile.sh file.exist file.notexist
The output was:
file.exist
file.notexist
file.notexist (arg2) is not a file
Then I ran bash isFile.sh "" file.notexist
The output was:
(# empty line)
file.notexist
file.notexist (arg2) is not a file
Expected output is:
(# empty line)
file.notexist
(arg1) is not a file
file.notexist (arg2) is not a file
Can somebody explain why?
files string test
The script is called isFile.sh and looks like this:
#!/bin/sh
echo $1
echo $2
if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi
if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi
First I created a file by doing touch file.exist
.
And I ran bash isFile.sh file.exist file.notexist
The output was:
file.exist
file.notexist
file.notexist (arg2) is not a file
Then I ran bash isFile.sh "" file.notexist
The output was:
(# empty line)
file.notexist
file.notexist (arg2) is not a file
Expected output is:
(# empty line)
file.notexist
(arg1) is not a file
file.notexist (arg2) is not a file
Can somebody explain why?
files string test
files string test
edited Aug 7 at 6:04
asked Aug 7 at 4:44
SoloKyo
264
264
Are you sure aboutecho$1
? I would think it isecho $1
(better cut an paste). And welcome!
â Volker Siegel
Aug 7 at 6:01
@ Volker Siegel, Sure it'secho $1
, I edited my question, thanks.
â SoloKyo
Aug 7 at 6:04
add a comment |Â
Are you sure aboutecho$1
? I would think it isecho $1
(better cut an paste). And welcome!
â Volker Siegel
Aug 7 at 6:01
@ Volker Siegel, Sure it'secho $1
, I edited my question, thanks.
â SoloKyo
Aug 7 at 6:04
Are you sure about
echo$1
? I would think it is echo $1
(better cut an paste). And welcome!â Volker Siegel
Aug 7 at 6:01
Are you sure about
echo$1
? I would think it is echo $1
(better cut an paste). And welcome!â Volker Siegel
Aug 7 at 6:01
@ Volker Siegel, Sure it's
echo $1
, I edited my question, thanks.â SoloKyo
Aug 7 at 6:04
@ Volker Siegel, Sure it's
echo $1
, I edited my question, thanks.â SoloKyo
Aug 7 at 6:04
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
The issue is that [ ! -f $1 ]
becomes [ ! -f ]
after expansion (and not [ ! -f "" ]
as you thought!), so instead if checking if a given file exists, [
checks if the -f
string is empty or not. It's not empty, but thanks to !
the final exit code is 1
, thus the echo
command is not executed.
That's why you need to quote your variables in POSIX shells.
Related questions:
- Why does my shell script choke on whitespace or other special characters?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
@KitisinKyo, do you mean thatbash -c '[ -f "" ] && echo yes'
outputsyes
for you? What system is that?
â Stéphane Chazelas
Aug 7 at 5:37
@Stéphane Chazelas, Nope,bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7
â SoloKyo
Aug 7 at 5:58
@KitisinKyo, yet you're saying in the comment above thatif [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script withbash -x
to see what happens.
â Stéphane Chazelas
Aug 7 at 6:00
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The issue is that [ ! -f $1 ]
becomes [ ! -f ]
after expansion (and not [ ! -f "" ]
as you thought!), so instead if checking if a given file exists, [
checks if the -f
string is empty or not. It's not empty, but thanks to !
the final exit code is 1
, thus the echo
command is not executed.
That's why you need to quote your variables in POSIX shells.
Related questions:
- Why does my shell script choke on whitespace or other special characters?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
@KitisinKyo, do you mean thatbash -c '[ -f "" ] && echo yes'
outputsyes
for you? What system is that?
â Stéphane Chazelas
Aug 7 at 5:37
@Stéphane Chazelas, Nope,bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7
â SoloKyo
Aug 7 at 5:58
@KitisinKyo, yet you're saying in the comment above thatif [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script withbash -x
to see what happens.
â Stéphane Chazelas
Aug 7 at 6:00
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
 |Â
show 1 more comment
up vote
4
down vote
accepted
The issue is that [ ! -f $1 ]
becomes [ ! -f ]
after expansion (and not [ ! -f "" ]
as you thought!), so instead if checking if a given file exists, [
checks if the -f
string is empty or not. It's not empty, but thanks to !
the final exit code is 1
, thus the echo
command is not executed.
That's why you need to quote your variables in POSIX shells.
Related questions:
- Why does my shell script choke on whitespace or other special characters?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
@KitisinKyo, do you mean thatbash -c '[ -f "" ] && echo yes'
outputsyes
for you? What system is that?
â Stéphane Chazelas
Aug 7 at 5:37
@Stéphane Chazelas, Nope,bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7
â SoloKyo
Aug 7 at 5:58
@KitisinKyo, yet you're saying in the comment above thatif [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script withbash -x
to see what happens.
â Stéphane Chazelas
Aug 7 at 6:00
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
 |Â
show 1 more comment
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The issue is that [ ! -f $1 ]
becomes [ ! -f ]
after expansion (and not [ ! -f "" ]
as you thought!), so instead if checking if a given file exists, [
checks if the -f
string is empty or not. It's not empty, but thanks to !
the final exit code is 1
, thus the echo
command is not executed.
That's why you need to quote your variables in POSIX shells.
Related questions:
- Why does my shell script choke on whitespace or other special characters?
- Security implications of forgetting to quote a variable in bash/POSIX shells
The issue is that [ ! -f $1 ]
becomes [ ! -f ]
after expansion (and not [ ! -f "" ]
as you thought!), so instead if checking if a given file exists, [
checks if the -f
string is empty or not. It's not empty, but thanks to !
the final exit code is 1
, thus the echo
command is not executed.
That's why you need to quote your variables in POSIX shells.
Related questions:
- Why does my shell script choke on whitespace or other special characters?
- Security implications of forgetting to quote a variable in bash/POSIX shells
answered Aug 7 at 4:58
nxnev
2,4872423
2,4872423
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
@KitisinKyo, do you mean thatbash -c '[ -f "" ] && echo yes'
outputsyes
for you? What system is that?
â Stéphane Chazelas
Aug 7 at 5:37
@Stéphane Chazelas, Nope,bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7
â SoloKyo
Aug 7 at 5:58
@KitisinKyo, yet you're saying in the comment above thatif [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script withbash -x
to see what happens.
â Stéphane Chazelas
Aug 7 at 6:00
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
 |Â
show 1 more comment
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
@KitisinKyo, do you mean thatbash -c '[ -f "" ] && echo yes'
outputsyes
for you? What system is that?
â Stéphane Chazelas
Aug 7 at 5:37
@Stéphane Chazelas, Nope,bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7
â SoloKyo
Aug 7 at 5:58
@KitisinKyo, yet you're saying in the comment above thatif [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script withbash -x
to see what happens.
â Stéphane Chazelas
Aug 7 at 6:00
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
â SoloKyo
Aug 7 at 5:16
@KitisinKyo, do you mean that
bash -c '[ -f "" ] && echo yes'
outputs yes
for you? What system is that?â Stéphane Chazelas
Aug 7 at 5:37
@KitisinKyo, do you mean that
bash -c '[ -f "" ] && echo yes'
outputs yes
for you? What system is that?â Stéphane Chazelas
Aug 7 at 5:37
@Stéphane Chazelas, Nope,
bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7â SoloKyo
Aug 7 at 5:58
@Stéphane Chazelas, Nope,
bash -c '[ -f "" ] && echo yes'
had no output. I'm using CentOS 7â SoloKyo
Aug 7 at 5:58
@KitisinKyo, yet you're saying in the comment above that
if [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script with bash -x
to see what happens.â Stéphane Chazelas
Aug 7 at 6:00
@KitisinKyo, yet you're saying in the comment above that
if [ ! -f "" ]; then echo ...; fi
outputs nothing. Try running the script with bash -x
to see what happens.â Stéphane Chazelas
Aug 7 at 6:00
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
â SoloKyo
Aug 7 at 6:13
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460950%2fempty-string-is-a-file-if-f%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Are you sure about
echo$1
? I would think it isecho $1
(better cut an paste). And welcome!â Volker Siegel
Aug 7 at 6:01
@ Volker Siegel, Sure it's
echo $1
, I edited my question, thanks.â SoloKyo
Aug 7 at 6:04