bad interpreter error message when trying to run awk executable
From a command line, run this command:
which awk
This will print the path of AWK, which is likely /usr/bin/awk
. Correct the first line and your script should work.
Also, your script shouldn't have the single-quote characters at the beginning and end. You can run AWK from the command line and pass in a script as a quoted string, or you can write a script in a file and use the #!/usr/bin/awk
first line, with the commands just in the file.
Also, the first line of your script isn't going to work right. In AWK, setup code needs to be inside the BEGIN
block, and $1
is a reference to the first word in the input line. You need to use ARGV[1]
to refer to the first argument.
http://www.gnu.org/software/gawk/manual/html_node/ARGC-and-ARGV.html
As @TrueY pointed out, there should be a -f
on the first line:
#!/usr/bin/awk -f
This is discussed here: Invoking a script, which has an awk shebang, with parameters (vars)
Working, tested version of the program:
#!/usr/bin/awk -f
## pick1 - pick one random number out of y
## main routine
BEGIN {
TOPNUM = ARGV[1]
## set seed
srand ()
## get a random number
select = 1 +int(rand() * TOPNUM)
# print pick
print select
}
Bash script and /bin/bash^M: bad interpreter: No such file or directory
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.
Try running dos2unix
on the script:
http://dos2unix.sourceforge.net/
Or just rewrite the script in your Unix env using vi
and test.
Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.
If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.
In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows
. To change it go to
- settings->preferences
- new document / default directory tab
- select the format as unix and close
- create a new document
shell script: bad interpreter: No such file or directory when using pwd
Better do :
#!/bin/bash
count=0
dir="$PWD"
echo "$dir"
for file in "$dir"/*
do
if [[ -f $file ]]
then
((count++))
fi
done
echo $count
or a simplest/shortest solution :
#!/bin/bash
echo "$PWD"
for file; do
[[ -f $file ]] && ((count++))
done
echo $count
./configure : /bin/sh^M : bad interpreter
To fix, open your script with vi or vim and enter in vi command mode (key Esc), then type this:
:set fileformat=unix
Finally save it
:x!
or :wq!
env: bash\r: No such file or directory
The error message suggests that the script you're invoking has embedded \r
characters, which in turn suggests that it has Windows-style \r\n
line endings instead of the \n
-only line endings bash
expects.
As a quick fix, you can remove the \r
chars. as follows:
sed $'s/\r$//' ./install.sh > ./install.Unix.sh
Note: The $'...'
string is an ANSI-C quoted string supported in bash
, ksh
, and zsh
. It is used to ensure that the \r
expands to an actual CR character before sed
sees the script, because not all sed
implementations themselves support \r
as an escape sequence.
and then run
./install.Unix.sh --clang-completer
However, the larger question is why you've ended up with \r\n
-style files - most likely, other files are affected, too.
Perhaps you're running Git on Windows, where a typical configuration is to convert Unix-style \n
-only line breaks to Windows-style \r\n
line breaks on checking files out and re-converting to \n
-only line breaks on committing.
While this makes sense for development on Windows, it gets in the way of installation scenarios like these.
To make Git check out files with Unix-style file endings on Windows - at least temporarily - use:
git config --global core.autocrlf false
Then run your installation commands involving git clone
again.
To restore Git's behavior later, run git config --global core.autocrlf true
.
syntax error when using command line in python
Looks like your problem is that you are trying to run python test.py
from within the Python interpreter, which is why you're seeing that traceback.
Make sure you're out of the interpreter, then run the python test.py
command from bash or command prompt or whatever.
Invoking a script, which has an awk shebang, with parameters (vars)
Try using:
#!/usr/bin/awk -f
as an interpreter
Something is wrong by compilation
UPDATE: After chatting with OP(which I mentioned in my comments too) OP needs to change function's name to actual function from t
and it worked then. Thought to update here so all will know it.
There could be 2 possible solutions.
1st: Mention awk
in shellscript and run it as shell script.
cat script.ksh
awk 'BEGIN { CONVFMT="%0.17f" }
/D Format/ { in_f_format=0; next }
/F Format/ { in_f_format=1; next }
in_f_format != 1 { next }
!($1 ~ /^[1-9]/) { next }
$1 == 11 { prt(180,3.141592653589); next }
$1 == 15 { prt(100,1); next }
$1 == 20 { prt(10,1); next }
$1 == 26 { next }
{ prt(1,1) }
function prt(mult, div) {
print trunc($5 * mult / div) ORS trunc($6 * mult / div)
}
function trunc(n, s) {
s=index(n,".")
return (s ? substr(n,1,s+6) : n)
}' Input_file
Give script.ksh
proper execute permissions and run it like you are running.
2nd: Run it as a awk
script by running it like:
awk -f awk_file Input_file
Related Topics
Resolve Relative Relocations in Partial Link
Apache Proxypass Not Loading Resources
How to Merge Two Seperate - Yet Similar - Codebases into One Svn Rep
Bash - Concatenating Variable on to Path
Sched_Fifo Higher Priority Thread Is Getting Preempted by the Sched_Fifo Lower Priority Thread
How to Set Firefox Binary Path of Firefox in Selenium in Linux
Libreoffice Command Line Conversion - No Output File
Differencebetween These Two Commands Which Are Used to Run Shell Script
Diff (Gnu Diffutils) 3.6 Exclude Directory
Compile Git for 32-Bit Linux on Shared Hosting
How to Inform Gcc to Not Use a Particular Register
How to Make a Bash String of Command with Redirect and Pipe
Batch Remove Substring from Filename with Special Characters in Bash
File Glob Patterns in Linux Terminal
Run Bash Script in Raspberry Pi via Ifttt
How to Make Git-Bash Command Line Start Up with Home Directory