Cannot execute shell commands in bash script

user
 #!/bin/bash
#general security monitoring
PATH=/var/log
echo "The IP addresses of users with more than 2 failed login attempts are:"
IPFAILEDLOGINS=$(grep "Failed password" /var/log/secure | cut -d: -f4 | awk '{print $6}' | uniq -c | awk '{if ($1>=2) print $2}')
echo "$IPFAILEDLOGINS"

RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
echo "The current rsyslog clients are: $RSYSLOGCLIENTS"

error: ./securityanalysis.sh: line 7: find: command not found

find is located under /bin, which is included in my PATH. I also put the directory this script was being executed in into the PATH but it didn't make a difference.

Replacing the echo.. line with eval $RSYSLOGCLIENTS also gave me the same error.

Can someone please explain what is happening?

Note: I assume this is extremely bad practice, but this script is located in the home directory of root. Could this have something to do with it?

John1024

find is located under /bin, which is included in my PATH

No, it isn't. Your PATH is redefined in line 3 of the script to be:

PATH=/var/log

Observe that the find command works before but not after PATH is reassigned:

$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
$ PATH=/var/log
$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
bash: find: command not found

The general lesson here is, when defining shell variables for your script, never use all capitals. The shell uses all caps for its important variables, like PATH. You don't want to overwrite them. Use lower or, at least, mixed case for your internal script variables.

For example, the path variable is assigned a value and it does not affect the ability of the shell to find find:

$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
$ path=/var/log
$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
$

In shell, variable names are case-sensitive and, therefore, PATH and path are separate and independent variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shell out into zsh and execute commands from bash script

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

Execute gcloud commands in a bash script

From Dev

Bash script to spawn and execute commands

From Dev

Can a bash shell script open a text file with less and then execute less commands from the script?

From Dev

Maven: Cannot execute shell script

From Dev

How to execute the vim commands through shell script

From Dev

Make a shell script execute commands in telnet or programs

From Dev

How to execute commands in gnuplot using shell script?

From Dev

how to execute compound commands in shell script?

From Dev

How to execute git commands from bash script?

From Dev

bash script execute commands after ssh

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

How to execute the following commands in a bash script?

From Dev

Bash script to execute multiple Unix commands

From Dev

Windows Git Bash shell cannot find commands?

From Dev

Shell commands not working when grouped in a bash script

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

Cannot execute a shell script from gradle file

From Dev

Execute a bash script the first time shell opens

From Dev

Execute script in sh mode instead of bash in Shell

From Dev

Execute script in sh mode instead of bash in Shell

From Dev

Execute command with backquote in bash shell script

From Dev

Cannot execute Bash script from PHP

From Dev

Cannot execute telnet commands using PHP shell_exec()

From Dev

A better way to execute multiple MySQL commands using shell script

From Dev

Can't execute sqlplus commands in shell script created by PHP page

Related Related

  1. 1

    Shell out into zsh and execute commands from bash script

  2. 2

    How to execute commands in docker container as part of bash shell script

  3. 3

    How to execute shell commands in a loop from within a bash script?

  4. 4

    Execute gcloud commands in a bash script

  5. 5

    Bash script to spawn and execute commands

  6. 6

    Can a bash shell script open a text file with less and then execute less commands from the script?

  7. 7

    Maven: Cannot execute shell script

  8. 8

    How to execute the vim commands through shell script

  9. 9

    Make a shell script execute commands in telnet or programs

  10. 10

    How to execute commands in gnuplot using shell script?

  11. 11

    how to execute compound commands in shell script?

  12. 12

    How to execute git commands from bash script?

  13. 13

    bash script execute commands after ssh

  14. 14

    Bash script to read from a file and execute commands

  15. 15

    Bash script to read from a file and execute commands

  16. 16

    How to execute the following commands in a bash script?

  17. 17

    Bash script to execute multiple Unix commands

  18. 18

    Windows Git Bash shell cannot find commands?

  19. 19

    Shell commands not working when grouped in a bash script

  20. 20

    How to execute sudo commands with Expect & send commands in bash script?

  21. 21

    Cannot execute a shell script from gradle file

  22. 22

    Execute a bash script the first time shell opens

  23. 23

    Execute script in sh mode instead of bash in Shell

  24. 24

    Execute script in sh mode instead of bash in Shell

  25. 25

    Execute command with backquote in bash shell script

  26. 26

    Cannot execute Bash script from PHP

  27. 27

    Cannot execute telnet commands using PHP shell_exec()

  28. 28

    A better way to execute multiple MySQL commands using shell script

  29. 29

    Can't execute sqlplus commands in shell script created by PHP page

HotTag

Archive