Error not being obtained in file - linux

Probably a naive question but I am stuck.
I have a shell script file.sh which contains a simple command(I don't have python installed)
echo $PATH && cd /home/akash/repos/dhvani/ && echo $PWD && python main.py
When I run it as sh file.sh > /tmp/out. I get the the portions of echo in the /tmp/out but not the error. I don't have python installed so I must get error like python: not found in the file as well. But I only get that in the terminal.
Can anyone explain a way how can I log this error as well.

To make stderr as well as stdout go to the file, do this:
sh file.sh > /tmp/out 2>&1

Related

Shell script fucntion oputput [duplicate]

This is what was trying to do:
$ wget -qO- www.example.com/script.sh | sh
which quietly downloads the script and prints it to stdout which is then piped to sh. This unfortunately doesn't quite work, failing to wait for user input at various points, aswell as a few syntax errors.
This is what actually works:
$ wget -qOscript www.example.com/script.sh && chmod +x ./script && ./script
But what's the difference?
I'm thinking maybe piping the file doesn't execute the file, but rather executes each line individually, but I'm new to this kind of thing so I don't know.
When you pipe to sh , stdin of that shell/script will be the pipe. Thus the script cannot take e.g. user input from the console. When you run the script normally, stdin is the console - where you can enter input.
You might try telling the shell to be interactive:
$ wget -qO- www.example.com/script.sh | sh -i
I had the same issue, and after tinkering and googling this is what worked for me.
wget -O - www.example.com/script.sh | sh

Piping a shell script to bash and launch interactive bash

Consider the following shell script on example.com
#/bin/bash
export HELLO_SCOPE=WORLD
eval $#
Now, I would like to download and then execute this shell script with parameters in the simplest way and be able to launch an interactive bash terminal with the HELLO_SCOPE variable set.
I have tried
curl http://example.com/hello_scope.sh | bash -s bash -i
But it quits the shell immediately. From what I can understand, it's because curls stdout, the script, remains the stdin of the bash, preventing it from starting interactively (as that would require my keyboard to be stdin).
Is there a way to avoid this without going through the extra step of creating a temporary file with the shell script?
You can source it:
# open a shell
. <(curl http://example.com/hello_scope.sh)
# type commands ...
You could just download this script you (using wget for example) and source this script, isn't it ?
script_name="hello_scope.sh"
[[ -f $script_name ]] && rm -rf "$script_name"
wget "http://example.com/$script_name" -O "$script_name" -o /dev/null
&& chmod u+x "$script_name"
&& source "$script_name"
You could use . "$script_name" instead of source "$script_name" if you want (. is POSIX compliant). You could write the previous code in a script and source it to have interactive shell with the setted variable $HELLO_SCOPE.
Finally you could remove the eval line in your remote shell script.

How to run matlab code in linux as script file?

I am looking into running matlab script in Linux similar to bash/python scripts. I.e., a matlab script that can be run as an application.
You can get a similar effect without your custom mash script by adding the following header to the files you want to be executable:
#/usr/bin/bash
/path/to/matlab -r "$(sed -n -e '4,$p' < "$0")"
exit $?
If you want matlab to terminate after executing the script, as in your example, you could replace the second line with
sed -n -e '4,$p' < "$0" | /path/to/matlab
The idea here is to execute a bash command that simply clips off the header of the script, and passes the rest along to matlab.
Here is the implementation I came up with -
Create /usr/bin/mash script file containing the following lines -
#!/bin/bash
grep -ve '^(#!\|^\s*$)' ${#: -1} | ${#: 1:$#-1}
exit $?
Make mash script executable -
$ chmod +x /usr/bin/mash
Write matlab script file called test.msh
#!/usr/bin/mash /usr/local/MATLAB/R2012a/bin/matlab -nodisplay
format long
a = 2*pi % matlab commands ...
Make test.msh script executable -
$ chmod +x mash
Run test.msh
$ ./test.msh
...
>> >> a =
6.283185307179586

Bash script change working directory to the directory it is in

I read Can a Bash script tell what directory it's stored in?, it shows I can get script directory by DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )".
I find I can use cd command to change working directory.
This is the content of import.sh. It is in /Users/gqqnbig/SourceCode/Java/PlayerStatisticReader/bin.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd DIR
java -cp .;jsoup-1.7.3.jar;mysql-connector-java-5.1.29-bin.jar globalVisionEntertainment.nba.Program %1
This is what I get after executing the script.
Macintosh:PlayerStatisticReader gqqnbig$ pwd
/Users/gqqnbig/SourceCode/Java/PlayerStatisticReader
Macintosh:PlayerStatisticReader gqqnbig$ bin/import.sh
: command not found 2:
: No such file or directoryDIR
: command not found 4:
I execute it in the default terminal in Macintosh.
Why is the command not found? How can I make it work?
you need to write
cd "$DIR"
strictly, you only need to add the dollar, but you should also quote the path because it may contain spaces. as to the command not found messages; I don't know. You can remove the empty lines. my guess is it's an encoding issue. do you get the "command not found" output if you paste the script directly into your terminal instead of running the file?
use the following to expand the variable
cd ${DIR}
For what it's worth, I was looking for a solution on how to schedule a command with specific work directory via crontab schedule.
I found a way to do it in one line with env.
env -C workdir - command args

Executing shell script in ubuntu

Maybe I am missing something here but I have the following small bash script to delete some old files that get created when using flex and yacc its pretty simple but when I run the script it echos the result to the terminal but does not delete the file I'm probably missing something stupid could you guys point me in the right direction.
#!/bin/bash
echo "rm -f y.tab.h"; (just using one file for now)
I tried changing it to
echo rm -f y.tab.h;
but still no luck
I tried executing it with bash delete.sh and sh delete.sh and even using chmod +x on the file and executing it with ./
Remove echo statement
Just use
rm -f y.tab.h

Resources