0

I have a bash that reads a directory and tries to cat a file called "test" in each. "test" only exists in the directory called "IOEAdmin" please disregard the other folders in the screenshot.

Here is the bash script

#!/bin/bash

function deploy() {
        WD="$(pwd)"
        if [[ "$1" = "all" ]]
        then
                for dir in webapps/* ; do
                        if [[ "$dir" == *"IOE"* ]]
                        then
                                "cat $WD/$dir/test"
                        #       "rm -r -f $WD/$dir"
                        #       projectName=$(echo $dir| cut -d'/' -f 2)
                        #       "cp -r $WD/ioe_production/$projectName $WD/$dir"
                        fi
                done
        fi
}
deploy $1

it returns

[tomcat@llsrchwebqa3 tomcat]$ sh deploy.sh all
deploy.sh: line 15: cat /services/tomcat/ioe/tomcat/webapps/IOEAdmin/test: No such file or directory

I then immediately copy the path from the error and run cat on it

[tomcat@llsrchwebqa3 tomcat]$ cat /services/tomcat/ioe/tomcat/webapps/IOEAdmin/test
hello world
[tomcat@llsrchwebqa3 tomcat]$

Here's the entire sequence (ignore the other projects besides IOEAdmin)

[tomcat@llsrchwebqa3 tomcat]$ sh deploy.sh all
all

deploy.sh
/services/tomcat/ioe/tomcat
webapps/IOEAdmin
deploy.sh: line 15: cat /services/tomcat/ioe/tomcat/webapps/IOEAdmin/test: No such file or directory
webapps/IOEPermissions
deploy.sh: line 15: cat /services/tomcat/ioe/tomcat/webapps/IOEPermissions/test: No such file or directory
webapps/IOEReview
deploy.sh: line 15: cat /services/tomcat/ioe/tomcat/webapps/IOEReview/test: No such file or directory
webapps/IOETime
deploy.sh: line 15: cat /services/tomcat/ioe/tomcat/webapps/IOETime/test: No such file or directory
[tomcat@llsrchwebqa3 tomcat]$ cat /services/tomcat/ioe/tomcat/webapps/IOEAdmin/test
hello world
[tomcat@llsrchwebqa3 tomcat]$

"test" was created in linux so I don't think it's a CRLF/LF end of file issue.

Why can't my bash script execute commands like cat, cp, and rm on the "test" file?

Paul Sender
  • 376
  • 2
  • 19
  • 2
    When you put `"cat filename"` in quotes, it's looking for something like `/usr/bin/cat filename` (with the space and the argument as part of the executable name) instead of `/usr/bin/cat`. Only use quotes (and _always_ use quotes) when you want a variable's expansion to be treated as part of a single word. – Charles Duffy Jul 20 '21 at 15:50
  • 2
    Now, what you _should_ be doing here is `cat "$WD/$dir/$test"` -- with quotes around the _argument_ so it can't be split into multiple arguments -- but those quotes shouldn't also enclose the `cat`. – Charles Duffy Jul 20 '21 at 15:52

0 Answers0