STDIN from Makefile with make run command - python-3.x

I have a program in which I'm trying to invoke the following command:
make run {FILE} e.g filename.txt
How can I create my Makefile so it can execute this program and accept any filename through STDIN.
My Makefile looks like the following:
run:
python execute.py --filename {ANY FILENAME}

The arguments to make are targets. There's no special relationship between run and {FILE} in your initial example. If you want to force the run prefix, it'll have to be part of the target name, instead of space separated:
run-%:
python execute.py --filename $*

Related

How to call a variable in a bash command line?

I am trying to create this little program to help me, with only one command, to compile and run a C program within Ubuntu's terminal.
Trying to make it fancier, I added an argument to the bash file so I can use it for any C program I want. So this is how it is supposed to go:
Create a variable to store the name of the file
Use that variable to compile the program (to the same file name)
Use that same name to run the file.
Here is the code:
# usr/bin/bash
filename=$1
cc -o $filename "$filename.c"
./$filename.out
almost everything runs, the only problem I still have is in the last line:
./$filename.out
It doesn't seem to use the name of the variable inside the command which executes the final program.
I'm a noob at bash (let's say I haven't used it in months).
cc -o foo will output foo not foo.out. You should also double-quote the variable expansions to prevent IFS-splitting and globbing:
filename=$1
cc -o "$filename" "$filename.c" &&
./"$filename"
Apart from that # /usr/bin/bash (unlike #!/usr/bin/bash) does nothing. It's a comment. The whole thing will be run the /bin/sh, not bash (but you don't need bash, anyway).
You will need to use quotation marks:
./"$filename.out"
#!/bin/sh
filename=$1
cc -o "${filename}.out" "${filename}.c"
"./${filename}.out"

LD_PRELOAD in rlwrap?

When I do something in my script like
rlwrap -f words.txt LD_PRELOAD=mylib.so command "$#"
I always get something like
rlwrap: error: cannot execute LD_PRELOAD=mylib.so no such file or directory
Even though that file totally exists and removing rlwrap would work just fine.
How do I put LD_PRELOAD in rlwrap? basically I want to make mylib.so apply to my 'command' only.
I did try putting LD_PRELOAD=mylib.so in from of rlwrap, it runs, but LD_PRELOAD didn't apply to command as I wished.
You can wrap the command in a shell:
rlwrap -f words.txt bash -c 'LD_PRELOAD=mylib.so command "$#"' - "$#"

Anyway to change the default exec call length?

I have a bash script and will have the first line start with # and followed by the command to execute the script, and it seems the limitation is 80 characters due to the exec call has such limitation, is there anyway to change that ? because sometimes my path will be very long.
Update.
My case is that I use virtualenv to generate a clean python environment. And in this environment, there's one executable file called pip, the shebang line is python executable path and sometimes this path will be very long, e.g.
#!/Users/myname/github/myproject/virtualenv_python3.4/bin/python3.4
If you don't want to modify your path to include the directory in which the executable, you can create a simple wrapper:
#!/bin/bash
/Users/myname/github/myproject/virtualenv_python3.4/bin/python3.4 <(cat <<"EOF"
# Python script goes here
EOF) "$#"

Suppress echo of command invocation in makefile?

I wrote a program for an assignment which is supposed to print its output to stdout. The assignment spec requires the creation of a Makefile which when invoked as make run > outputFile should run the program and write the output to a file, which has a SHA1 fingerprint identical to the one given in the spec.
My problem is that my makefile:
...
run:
java myprogram
also prints the command which runs my program (e.g. java myprogram) to the output file, so that my file includes this extra line causing the fingerprint to be wrong.
Is there any way to execute a command without the command invocation echoing to the command line?
Add # to the beginning of command to tell gmake not to print the command being executed. Like this:
run:
#java myprogram
As Oli suggested, this is a feature of Make and not of Bash.
On the other hand, Bash will never echo commands being executed unless you tell it to do so explicitly (i.e. with -x option).
Even simpler, use make -s (silent mode)!
You can also use .SILENT
.SILENT: run
hi:
echo "Hola!"
run:
java myprogram
In this case, make hi will output command, but make run will not output.
The effect of preceding the command with an # can be extended to a section by extending the command using a trailing backslash on the line. If a .PHONY command is desired to suppress output one can begin the section with:
#printf "..."

Linux: Run a binary in a script

i want to run a program via script.
normally i type ./program in the shell and the program starts.
my script looks like this:
#!/bin/sh
cd /home/user/path_to_the_program/
sh program
it fails, i think the last line went wrong...
i know this is childish question but thx a lot!
If ./program works in the shell, why not use it in your script?
#!/bin/sh
cd /home/user/path_to_the_program/
./program
sh program launches sh to try and interpret program as a shell script. Most likely it's not a script but some other executable file, which is why it fails.
When you type
./program
The shell tries to execute the program according to how it determines the file needs to be executed. If it is a binary, it will attempt to execute the entry subroutine. If the shell detects it is a script, e.g through the use of
#!/bin/sh
or
#!/bin/awk
or more generally
#!/path/to/interpreter
the shell will pass the file (and any supplied arguments) as arguments to the supplied interpreter, which will then execute the script. If the interpreter given in the path does not exist, the shell will error, and if no interpreter line is found, the shell will assume the supplied script is to executed by itself.
A command
sh program
is equivalent to
./program
when the first line of program contains
#!/bin/sh
assuming that /bin/sh is the sh in your path (it could be /system/bin/sh, for example). Passing a binary to sh will cause sh to treat it as a shell script, which it is not, and binary is not interpretable shell (which is plain text). That is why you cannot use
sh program
in this context. It will also fail due to program being ruby, awk, sed, or anything else that is not a shell script.
You don't need the sh and looks like you don't have the path to the program in your $PATH.
Try this:
#!/bin/sh
cd /home/user/path_to_the_program/
./program
You don't need the "sh" here. Just put "program" on the last line by itself.
This should be enough:
/home/user/path_to_the_program/program
If that does not work, check the following:
executable bit
shebang line of the program (if it is a script)

Resources