Cannot execute binary file with bash command - bash

I want to run a cpp executable from my git for windows bash. I do not understand why I can run it with ./Main but I can't run it with bash Main or bash Main.exe. In the latter cases, I'm getting an error:
cannot execute binary file
main.cpp
#include<iostream>
int main()
{
std::cout<<"Hello World";
return 0;
}
script.sh
echo "Hello from Bash script.."
echo "Hostname:${1}"
echo "Port:${2}"
echo "Listing contents:"
ls -a
echo "Launching cpp executable:"
path=$(pwd)
echo "Current path:${path}"
bash "${path}/Main"
To compile the C++ code, I'm using: g++ -o Main main.cpp.
What is the problem? Can someone explain please?

Just remove the bash on the last line of your script:
"${path}/Main"
Don't forget to make it executable.
chmod +x script.sh
It worked for me:
./script.sh hostname 80
Hello from Bash script..
Hostname:hostname
Port:80
Listing contents:
. .. Main main.cpp script.sh
Launching cpp executable:
Current path:/tmp/test
Hello World

Related

How do I compile and run a Rust file on save using entr?

How do I use entr to automatically compile and run a test Rust file on every save? I tried
ls test.rs | entr -c "rustc test.rs && ./test"
But it gives the error:
entr: exec rustc test.rs && ./test: No such file or directory
You need the -s argument, which uses your shell to evaluate the command:
-s Evaluate the first argument using the
interpreter specified by the SHELL
environment variable. When this flag
is set, the name of the shell and exit
code is printed after each invocation.
The correct command is:
ls test.rs | entr -cs "rustc test.rs && ./test"
As a bonus, it also prints the exit code for each invocation!
Hello World!
zsh returned exit code 0
Similarly for cargo, on any rust file change saved under src directory:
ls src/**/*.rs | entr -cs "cargo run"

Running executable file with additional options or arguments

I'm writing a bash script Test.sh that aims to execute anotherscript (a linux executable file):
#!/bin/bash -l
mp1='/my/path1/'
mp2='/my/path2/anotherscript'
for myfile in $mp1*.txt; do
echo "$myfile"
"$mp2 $myfile -m mymode"
echo "finished file"
done
Notice that anotherscript takes as arguments $myfile and options -m mymode.
But I get the file not found error (says Test.sh: line 8: /my.path2/anotherscript: No such file or directory).
My questions are:
I have followed this question to get my bash script to run the executable file. But I'm afraid I still get the error above.
Am I specifying arguments as they should to execute the file?
I suggest you use
sh -c "$mp2 $myfile -m mymode"
instead of just
"$mp2 $myfile -m mymode"
#!/bin/bash -l
dir=`find /my/path1/ -name "*.txt"`
mp2='/my/path2/anotherscript'
for myfile in "$dir"; do
echo "$myfile"
"$mp2" "$myfile" -m mymode
echo "finished file"
done
Make sure anotherscript has execution right (chmod +x anotherscript).

Rules of executing shell command in Makefile

When I executed command make, I got an error message
Makefile:4: *** missing separator. Stop.
The command in Makefile is:
$(shell ./makejce common/jce jce)
What's wrong with it?
-------makejce---------
#!/bin/bash
FLAGS=""
local_protoc=""
dir0=`pwd`
dir=`pwd`
......
if [ $# -gt 1 ]
then
mkdir -p $2
cd $2
dir=`pwd`
cd $dir0
fi
cd $1
jce_dir=`pwd`
#sub dir
for d in `ls -d */`
do
if [ -d $d ]
then
cd $d
for f in `find . -name '*.jce'`
do
${local_protoc} ${FLAGS} --dir=${dir} $f
done
cd $jce_dir
fi
done
#current dir
for f in `ls *.jce`
do
${local_protoc} ${FLAGS} --dir=${dir} $f
done
cd $dir0
-----makefile------
......
$(shell ./makejce common/jce jce)
......
With so little info it looks extremely bizarre (why are you running all the build steps in a shell script then invoking that script with a shell makefile function? The entire point of a makefile is to manage the build steps...) but without more information I'll just answer your specific question:
The make shell function works like backticks or $(...) in shell scripts: that is it runs the command in a shell and expands to the stdout of the command.
In your makefile if you have:
$(shell echo hi)
then it runs the shell command echo hi and expands to the stdout (i.e., hi). Then make will attempt to interpret that as some makefile text, because that's where you have put the function invocation (on a line all by itself). That's a syntax error because make doesn't know what to do with the string hi.
If you want to run a shell function then either (a) redirect its output so it doesn't output anything:
$(shell ...command... >/dev/null 2>&1)
or (b) capture the output somewhere that it won't bother make, such as in a variable like this:
_dummy := $(shell ...command...)
(by using := here we ensure the shell function is evaluated when the makefile is parsed).

Why am I getting if: Expression Syntax when I try to run this script?

I am currently trying to run a Unix Executable File in terminal (my shell is TCSH) I downloaded online and I keep getting the following error:
if: Expression Syntax
Here is the script I am trying to run:
if [ -f .1 ]
then
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
chmod 700 xrdcalc
./xrdcalc
else
platform=`uname`
echo
echo
echo "You are using \"xrdcalc\" for the first time on $platform , read the \"Readme.txt\" file and then proceed"
echo
echo
echo "Press enter...."
read char;
echo `date` > .1
mkdir .source
mv xrdcalc-1.1.c .source
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
./xrdcalc
fi
I have little experience with running scripts and I am sure it is an easy fix.
There are other issues with this code that indicate it was coded for traditional sh or bash.
Just put #!/bin/bash as the first line (using the correct path to your system's copy of bash), and it should work without other modifications.
Of course,
chmod 755 scriptName
is also required to "mark" the file as executable and if the file is saved to a directory not in the path, you need to either cd to the correct dir, or invoke as
/full/path/to/scriptName
If you're using a reduced version of Linux that doesn't have bash installed and you can't install it for some reason, then look for other 'Bourne Shell' derived shell processors, like ash, dash, ksh, sh .
IHTH

Makefile as an executable script with shebang?

Is it possible to create an executable script that would be interpreted by make?
I tried this:
#!/usr/bin/env make --makefile=/dev/stdin
main:
#echo Hello!
but it does not work - hangs until press Ctrl-c.
#!/usr/bin/make -f
main:
#echo Hello World!
Is normally all you need in a standard make file. The filename is implicitly passed as the last argument. /dev/stdin here is (usually) the tty. You can do the whole env thing if there's a reason to, but often there's no need.
ajw#rapunzel:~/code/videocc/tools > vi Makefile
ajw#rapunzel:~/code/videocc/tools > chmod a+x Makefile
ajw#rapunzel:~/code/videocc/tools > ./Makefile
Hello World!
The following adds a level of indirection but it's the best solution I've come up with for self-executing makefiles not called "makefile":
#!/bin/sh
exec make -f- "$#" << 'eof'
.PHONY: all
all:
#echo 'hello world!'
I'm trying to collect #! env hacks for each language / program here.

Resources