0

A simple bash script:

check_file() {
  if [ -f "$1" ]; then
   echo "File is good"
   #call other functions
  else
   echo "No such file!"
  fi
}
chieck_file "$1"

when I call my script passing a path to a file and that file has a whitespace or brackets in name, my shell script fails. sh ./bash_script.sh /home/user/foo/foo\ bar\(1\).txt

Output: No such file!

OS: RHEL8

Nodir Nasirov
  • 1,488
  • 3
  • 26
  • 44
  • It'll fail even with no whitespace. `[-f` is invalid; it __must__ be `[ -f`. Similarly, it needs to be `"$1" ]` with a space, not `"$1"]` – Charles Duffy Jul 15 '21 at 22:54
  • Also, it's `fi`, not `fe`. Consider making a habit of running your scripts through http://shellcheck.net/ and fixing everything it finds before asking a question here. – Charles Duffy Jul 15 '21 at 22:55
  • `chieck_file` -> `check_file` ********* `[-f "$1"]` -> `[ -f "$1" ]` – Michael Jul 15 '21 at 22:55
  • 1
    And `sh` is not `bash`. When you run `sh yourscript` on a modern system, it gets interpreted as a POSIX sh script, not a bash script -- that's a more limited shell with fewer features (even if `sh` is a symlink to bash, it turns off some features when run with that name). On systems that _aren't_ modern (ones that weren't built to honor the 1992 POSIX.2 specification), `sh` may even be Bourne, which is even less compatible with bash than POSIX sh is. – Charles Duffy Jul 15 '21 at 22:56
  • File is there, I edited the script adding whitespaces around `[]` – Nodir Nasirov Jul 15 '21 at 23:48

0 Answers0