Echoing time string to log file on Solaris? - unix

I am trying to echo the time a shell script executes with the following
EXECTIME=$(date)
echo "executed on: $EXECTIME" >> script.log
This was taken from a Unix tutorial but for some reason it is not working on a Solaris box with SunOS 5.10
The error I am getting is:
syntax error at line 2: `$EXECTIME=$' unexpected
Is there a difference in Unix and Solaris commands?
I am using usr/bin/bash

If you use csh, use following:
set EXECTIME=`date`
echo "executed on: $EXECTIME" >> script.log

You are not running bash but the legacy bourne shell /bin/sh.
Either replace the first line by:
EXECTIME=`date`
or set your script to use a modern shell like ksh or bash with adding this line which must be the first one of the script:
#!/bin/ksh
or
#!/bin/bash

Related

zsh on macOS Montery gives syntax error: invalid arithmetic operator

Regard this distilled example of the problem.
% echo 'echo "$((1.5*2))"' | zsh
3.
% echo 'echo "$((1.5*2))"' >x
% <x zsh
3.
% echo $SHELL
/bin/zsh
% chmod +x x
% ./x
./x: line 1: 1.5*2: syntax error: invalid arithmetic operator (error token is ".5*2")
I understand this error in bash.
If I change the script to just
echo "$SHELL"
it emits /bin/zsh as expected when executed. So I have no reason to expect bash behaviour here.
Someone explain why zsh is acting like sh/bash but identifying as zsh? I'm not interested in a work around (I have several), I want to understand this. Thanks.
Short answer: zsh is not executing the script; bash is.
$SHELL is the name of your login shell, not the currently executing shell. Unlike bash, which will use itself to execute a script with no shebang, zsh uses /bin/sh; from man zshmisc:
If execution fails because the file is not in executable format, and
the file is not a directory, it is assumed to be a shell script.
/bin/sh is spawned to execute it.
In your case, /bin/sh is a shell (either bash or dash, most likely) that does not support floating-point arithmetic.

what is the first step of ssh copy execute in terminal?

I am using this command to copy file from remote server to local machine:
scp -r app:/home/dolphin/model* .
In bash it works fine.In zsh it throw this error:zsh: no matches found: app:/home/dolphin/model*.I am searching from Google and understand the bash and zsh have different rule of glob.Here is my question:
what is the execute step detail of this command?
anyone could tell me the shell how to execute the command,the first step is echo the path of this command?
I could use -v(verbose) to see the scp execute process.
I am unfamiliar with Zsh, but as far as I can say, Bash will pass the original string to the program as an argument if nothing is globbed, while it appears that Zsh complains in this case.
To ensure the "unglobbed" string is passed as an argument to scp(1), you can escape the asterisk:
scp -r app:/home/dolphin/model\* .
^^

Running a script according to shebang line

I've got a script on my computer named test.py. What I've been doing so far to run the program is type python test.py into the terminal.
Is there a command on Unix operating systems that doesn't require the user to specify the program he/she uses to run the script but that will instead run the script using whichever program the shebang line is pointing to?
For example, I'm looking for a command that would let me type some_command test.txtinto the terminal, and if the first line of test.txt is #!/usr/bin/python, the script would be interpreted as a python script, but if the first line is #!/path/to/javascript/interpreter, the the script would be interpreted as javascript.
This is the default behavior of the terminal (or just executing a file in general) all you have to do is make the script executable with
chmod u+x test.txt
Then (assuming text.txt is in your current directory) every time you type
./text.txt
It will look at the sh-bang line and use the program there to run text.txt.
If you really want to duplicate built-in functionality, try this.
#!/bin/sh
x=$1
shift
p=$(sed -n 's/^#!//p;q' "$x" | grep .) && exec $p "$#"
exec "$x" "$#"
echo "$0: $x: No can do" >&2
Maybe call it start to remind you of the similarly useful Windows command.

Unix mailx html mail not working

Following html mail using mailx command is working from shell terminal, but the same command is not working from shell script.
mailx -s "$(echo -e "${sub} TRP OF ${system} \nContent-Type: text/html")" example#gmail.com < TRP.html
I guess it is some small escape character error, but not sure what it is.
Can any one help here?
Perhaps your vars sub / system are only known in your current environment.
When your sript is called mymail, try
. mymail
(Start with a dot),
or first export your vars.
When these suggestions fail, debug:
use set -x or temporary put an "echo -e" in front of your line.

Whats the difference between running a shell script as ./script.sh and sh script.sh

I have a script that looks like this
#!/bin/bash
function something() {
echo "hello world!!"
}
something | tee logfile
I have set the execute permission on this file and when I try running the file like this
$./script.sh
it runs perfectly fine, but when I run it on the command line like this
$sh script.sh
It throws up an error. Why does this happen and what are the ways in which I can fix this.
Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn't support some of the extra features of Bash).
You can fix it by invoking it as bash script.sh, or if it's your machine you can change /bin/sh to be bash and not whatever it is currently (usually just by symlinking it - rm /bin/sh && ln -s /bin/bash /bin/sh). Or you can just use ./script.sh instead if that's already working ;)
If your shell is indeed dash and you want to modify the script to be compatible, https://wiki.ubuntu.com/DashAsBinSh has a helpful guide to the differences. In your sample it looks like you'd just have to remove the function keyword.
if your script is at your present working directory and you issue ./script.sh, the kernel will read the shebang (first line) and execute the shell interpreter that is defined. you can also call your script.sh by specifying the path of the interpreter eg
/bin/bash myscript.sh
/bin/sh myscript.sh
/bin/ksh myscript.sh etc
By the way, you can also put your shebang like this (if you don't want to specify full path)
#!/usr/bin/env sh
sh script.sh forces the script to be executed within the sh - shell.
while simply starting it from command line uses the shell-environemnt you're in.
Please post the error message for further answers.
Random though on what the error may be:
path specified in first line /bin/bash is wrong -- maybe bash is not installed?

Resources