Been banging my head against the wall for a couple hours so time to call in the experts. Writing a small script to run some reports on one of my office's systems and I was asked to take care of a Bash script for it. The program called "auto_rep" takes various options such as "-t" to run one task (to generate one type of report) and a "-1" to exit after one task. The options are separated by spaces when running the command from command-line. The command works directly from command line but I cannot get it to work from a script...
Below is the snippet of code causing me issues:
cmd=$(auto_rep -t createfin1report -1)
echo "running ${cmd} command..."
echo
eval $cmd
The problem is when I run the script, only the "auto_rep" part of the command (from $cmd variable) is run; basically running the program without any options. And it creates tons of reports without the "-t createfin1report -1" part of the command (yikes!). Glad I only tried it on our test system.
Anyone have any tips to help me out? Is my approach way off? BTW - had tried just storing the command in a non-array (cmd="auto_rep -t createfin1report -1") and that was causing me other headache with a "command not found" errors :)...
Thanks in advance!
Save output to an array, then executing this array.
declare -a cmd
cmd=( $(auto_rep -t createfin1report -1) )
echo Executing: "${cmd[#]}"
"${cmd[#]}"
Please make sure the output is a valid command, and spaces have been correctly placed in double-quotes.
This error happens when I run a software containing bash script with beggining like this:
#! /bin/sh
CYBER_UNAME=$(uname)
CYBER_UNAME_M=$(uname -m)
I tried to execute these two commands in terminal and it works fine. This error only happens when I run the shell script. What should I do?
The result of 'uname' is SunOS. This shell script cannot be modified since it's protected on our server.
The line
#! /bin/sh
should read:
#!/bin/bash
So, that script will probably never really work.
If you cannot modify the script in situ, you might want to copy it to your local directory and correct it.
Otherwise,
tail +2 scriptname|/bin/bash
might work.
I just installed MSYS2 on my Windows 7 VM and the only stuff I've really done so far was that I modified my ~/.bashrc file by adding a few lines.
The only stuff I added to the file was a few alias cmds, an export cmd, and I modified the PS1 variable. See below:
alias ll='ls -l'
alias la='ls -A'
alias cls='clear'
export PATH="$PATH:/home/Matt/bin"
PS1=$(print '\033]0;${PWD}\n\033[32m${USER}#${HOSTNAME}:\033[33m${PWD/${HOME}/\~}>\033[0m ')
Other then that stuff above, that's all I've really done. So after making the changes to ~/.bashrc I exited from MSYS2's command prompt and then restarted MSYS2 to make the .bashrc changes go into effect. I know I could have just re-sourced the file but I just quickly exited and restarted instead...
Then, after I restarted MSYS2 this printed as the first line in the terminal --> "Can't find file" and then after everytime I hit enter at the cmd prompt it prints "Can't find file" just before the next prompt prints to the screen, like below:
Terminal shows this when I start-up MSYS2:
--------------------------------------------------------------------
Can't find file
Matt#My-Win7VM:~> ls -l
total 0
drwxr-xr-x 1 My-Win7VM+Matt My-Win7VM+None 0 Jun 3 12:01 bin
Can't find file
Matt#My-Win7VM:~>
Can't find file
Matt#My-Win7VM:~>
Can't find file
Matt#My-Win7VM:~>
--------------------------------------------------------------------
Any ideas what file it could be talking about that it can't seem to find..?
Any thoughts or suggestions would be greatly appreciated.
Thanks in Advance,
Matt
Ok I think I found the problem.
I ran the env command, and the PS1 environment variable was showing this
PS1=Can't find file \033]0;${PWD}\n\033[1;31m${USER}#${HOSTNAME}:\033[1;34m${PWD/${HOME}/\~}>\033[0m
So I removed the export PS1=${print ...} line from my ~/.bashrc file to just a double quoted variable assignment, instead of using the print command.
Because it looks like the print command is trying to be run from "/c/Windows/system32/print" and if you pass it a string you can see the error that was showing:
$ which print
/c/Windows/system32/print
$ print "Hello World"
Can't find file Hello World
So as you can see, that print command is expecting a filename, and not a string. So switching it to just an assignment statement like below, fixed the error:
export PS1="\033[1;31m${USER}#${HOSTNAME}:\033[1;34m${PWD/${HOME}/\~}>\033[0m "
Now everything seems ok.
EDIT:
Ran into new problem where the directory in the prompt wasn't changing when I switched dirs... Found out I was using the wrong method. So I found the PS1 switches and now it's all better this time:
PS1="\033[1;31m\u#\h:\033[0m\033[1;34m\w>\033[0m "
-Matt
Looks like you've copied your $PS1 from /etc/profile
Try the $PS1 variable from /etc/bash.bashrc instead:
# Set a default prompt of: user#host, MSYSTEM variable, and current_directory
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u#\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\]\n\$ '
I am trying to Run Shell script in Cygwin but I am getting Following error msgs:
app.sh: line 215: clear: command not found
[: scripts/Test: binary operator expected.
The folder scripts/Test ID Not found...
Can Any one suggest. What is the problem Do i need to re install Cygwin again.
Because Same Script Running fine in the Linux Envirnment.
Thanks in Advance.....
the script is probably using '#!/bin/sh' but expects the behaviour of /bin/bash. Try executing the script as: /bin/bash shell_script.sh
debugging tools include executing the script with the -x option, i.e. bash -x shell_script.sh. The problem from the outset looks like an unset variable that is being checked using the unprotected form:
if [ $x = ]
the problem is that if $x is unset, then you end up with an empty token, which causes the script to fail.
for the explicit 'clear' command not found, the cygwin implementation of that is to call 'tput clear' if you replace the 'clear' call with 'tput clear' then it should work.
Every time I run a script using bash scriptname.sh from the command line in Debian, I get Command Not found and then the result of the script.
The script works but there is always a Command Not Found statement printed on screen for each empty line. Each blank line is resulting in a command not found.
I am running the script from the /var folder.
Here is the script:
#!/bin/bash
echo Hello World
I run it by typing the following:
bash testscript.sh
Why would this occur?
Make sure your first line is:
#!/bin/bash
Enter your path to bash if it is not /bin/bash
Try running:
dos2unix script.sh
That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF).
More details about the dos2unix command (man page)
Another way to tell if your file is in dos/Win format:
cat scriptname.sh | sed 's/\r/<CR>/'
The output will look something like this:
#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>
This will output the entire file text with <CR> displayed for each \r character in the file.
You can use bash -x scriptname.sh to trace it.
I also ran into a similar issue. The issue seems to be permissions. If you do an ls -l, you may be able to identify that your file may NOT have the execute bit turned on. This will NOT allow the script to execute. :)
As #artooro added in comment:
To fix that issue run chmod +x testscript.sh
This might be trivial and not related to the OP's question, but I often made this mistaken at the beginning when I was learning scripting
VAR_NAME = $(hostname)
echo "the hostname is ${VAR_NAME}"
This will produce 'command not found' response. The correct way is to eliminate the spaces
VAR_NAME=$(hostname)
On Bash for Windows I've tried incorrectly to run
run_me.sh
without ./ at the beginning and got the same error.
For people with Windows background the correct form looks redundant:
./run_me.sh
If the script does its job (relatively) well, then it's running okay. Your problem is probably a single line in the file referencing a program that's either not on the path, not installed, misspelled, or something similar.
One way is to place a set -x at the top of your script or run it with bash -x instead of just bash - this will output the lines before executing them and you usually just need to look at the command output immediately before the error to see what's causing the problem
If, as you say, it's the blank lines causing the problems, you might want to check what's actaully in them. Run:
od -xcb testscript.sh
and make sure there's no "invisible" funny characters like the CTRL-M (carriage return) you may get by using a Windows-type editor.
use dos2unix on your script file.
for executing that you must provide full path of that
for example
/home/Manuel/mywrittenscript
Try chmod u+x testscript.sh
I know it from here:
http://www.linuxquestions.org/questions/red-hat-31/running-shell-script-command-not-found-202062/
If you have Notepad++ and you get this .sh Error Message: "command not found"
or this autoconf Error Message "line 615:
../../autoconf/bin/autom4te: No such file or directory".
On your Notepad++, Go to Edit -> EOL Conversion then check Macinthos(CR).
This will edit your files. I also encourage to check all files with this command,
because soon such an error will occur.
Had the same problem. Unfortunately
dos2unix winfile.sh
bash: dos2unix: command not found
so I did this to convert.
awk '{ sub("\r$", ""); print }' winfile.sh > unixfile.sh
and then
bash unixfile.sh
Problems with running scripts may also be connected to bad formatting of multi-line commands, for example if you have a whitespace character after line-breaking "\". E.g. this:
./run_me.sh \
--with-some parameter
(please note the extra space after "\") will cause problems, but when you remove that space, it will run perfectly fine.
I was also having some of the Cannot execute command. Everything looked correct, but in fact I was having a non-breakable space right before my command which was ofcourse impossible to spot with the naked eye:
if [[ "true" ]]; then
highlight --syntax js "var i = 0;"
fi
Which, in Vim, looked like:
if [[ "true" ]]; then
highlight --syntax js "var i = 0;"
fi
Only after running the Bash script checker shellcheck did I find the problem.
I ran into this today, absentmindedly copying the dollar command prompt $ (ahead of a command string) into the script.
Make sure you havenĀ“t override the 'PATH' variable by mistake like this:
#!/bin/bash
PATH="/home/user/Pictures/"; # do NOT do this
This was my mistake.
Add the current directory ( . ) to PATH to be able to execute a script, just by typing in its name, that resides in the current directory:
PATH=.:$PATH
You may want to update you .bashrc and .bash_profile files with aliases to recognize the command you are entering.
.bashrc and .bash_profile files are hidden files probably located on your C: drive where you save your program files.