This question already has answers here:
read: Illegal option -d
(2 answers)
Closed 5 years ago.
The following is my code
Read file
Count=0
While read -n1 c
Do
Case $c in
.
.
.
.
Esac
Done < $file
Echo"$count"
When I run this code, it shows the error as
read: Illegal option -n
I'm just started learning shell programming.So please help me fix this code
-n is not an option for read in standard Unix sh and (some of) its variants.
read -n runs well on bash, zsh and ksh93, so you may want to select one of them instead of sh or dash (Debian sh), probably by adding a shebang line:
#! /bin/bash
Or run explicitly with bash:
bash foo.sh
Related
This question already has answers here:
Difference between sh and Bash
(11 answers)
Pattern matching in UNIX Case statement
(1 answer)
Closed 1 year ago.
I am using this image which has bash v4.3.48 and curl v7.56.1:
https://hub.docker.com/r/bizongroup/alpine-curl-bash/tags?page=1&ordering=last_updated
Inside the docker I write the following script:
email_dest="iz#gmail.com}}"
suffix="#gmail.com"
dm_to=${email_dest%"$suffix"}
if [[ $email_dest == *"#users.noreply.github.com"* ]]
then
echo "Email address is no reply. Please fix your email preferences in Github"
elif [[ $email_dest == *$suffix* ]]
then
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello <#'"$dm_to"'>. '{{inputs.parameters.workflow_name}}' "}' https://hooks.slack.com/services/T01JNE5DXA7/B0246T84N75/hHDk7RUg2BWl2bYbPoN9r
else
echo "Email address is not of digibank domain!"
fi
If I run this script with bash command <script_name> it will work as expected (Run the curl command). But if I run it with sh command <script_name> it will not run the curl command:
/ # bash send-message.sh
ok/ #
/ # sh send-message.sh
Email address is not of digibank domain!
Any suggestion of what it could be? and what should be changed so it will work with sh?
That lies within the differences between bash and sh:
sh is POSIX compliant, whereas bash isn't (fully).
As a best practice you should always include a shebang:
#!/usr/bin/env bash
echo "this is going to run within bash"
With this you can now omit calling the script via bash myscript and just call it with ./myscript and it is always going to use bash (even if you are in a zsh, sh or whatever else).
However, if you truly want to have a script that runs with both sh and bash then you should rewrite your script to be plain sh compliant (i.e. POSIX).
TL;DR
Any suggestion of what it could be? and what should be changed so it will work with sh?
In your script you are using bash extensions such as [[ which is why it does not work with sh.
Checkout the links I posted above for more differences and how you can "convert" your bash script into a sh script.
The following site has a great summary on what to change in order to get your bash script working for dash which is an implementation of sh: http://mywiki.wooledge.org/Bashism
Furthermore, you can also check if any issues exist by using the following site: https://www.shellcheck.net/
This question already has an answer here:
How to execute 4 shell scripts in parallel, I can't use GNU parallel?
(1 answer)
Closed 2 years ago.
I want to run multiple bash scripts in parallel.
example of my script running : ./test1.sh $1 and ./test2.sh $1
I tried this: parallel ::: "~/path/test1.sh $1" "~/path/test2.sh $1"
Not working properly, any idea how to fix this?
You could use xargs:
echo "~/path/test1.sh $1 ~/path/test2.sh $1" | xargs -P0 -n2 /bin/bash
-P0 says "run all in parallel"
-n2 passes two arguments to /bin/bash, in this case the script and the parameter
This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 4 years ago.
I have the following bash script test.sh (with execution permissions):
#!/bin/sh
CAT_BIN="cat"
"$CAT_BIN" <(tail -n +2 test.sh)
It gives me that error when I run it:
$ ./test.sh
./test.sh: line 4: syntax error near unexpected token `('
./test.sh: line 4: `"$CAT_BIN" <(tail -n +2 test.sh)'
However, when I source the following commands it executes alright.
$ CAT_BIN="cat"
$ "$CAT_BIN" <(tail -n +2 test.sh)
How can this work in a script? (Use <(tail -n +2 test.sh) inline as a filename argument)
The <(tail -n +2 test.sh) construct is a bash feature, so you need to run your script in the bash shell,
Replace your top line
#!/bin/sh
with
#!/bin/bash
(Or the proper path to the bash executable if it is not /bin/bash on your system)
Note, even if /bin/sh is e.g. a symlink to bash, it will start bash in posix compatibility mode when you run it as /bin/sh , and many bash specific features will not be available)
This question already has answers here:
Difference between sh and Bash
(11 answers)
String comparison in bash. [[: not found
(9 answers)
Closed 4 years ago.
I have a shell script where I want to check if it's being run by root. If it's not then I set the variable SUDO='sudo' to prepend to subsequent commands. That way the user gets one nice permission denied error message from the shell and if they're running with sudo, it properly does its business. However, for the following code I get:
myscript.sh: [: -ne: unexpected operator
Code is:
#!/bin/sh
SUDO=""
if [ $EUID -ne 0 ]; then
SUDO='sudo'
fi
I am running it like: sh myscript.sh as this is not a bash script. This is not an issue of sh vs bash afaik. The shebang says /bin/sh and I'm running it with sh.
This question already has an answer here:
Blank first line of shell script: explain behavior of UID variable
(1 answer)
Closed 6 years ago.
Hi I have a question about bash.
and I'm new to it.
I made a file named "test.sh" and its contents is
#!/bin/bash
set -x
echo $UID
echo "$UID"
echo "$(id -u)"
and the result is blank!!
nothing shows up
However, when i just type "echo $UID" on terminal
it shows "1011"
is there anything i missed for bash?
Please help
UPDATED
bash version is 4.3.11 and I typed "sh test.sh" to execute.
and the result is
+ echo
+ echo
+ id -u
+ echo 1011
1011
thanks!
$UID is a Bash variable that is not set under sh, that may be why it outputs blank lines.
Try bash test.sh or make your script executable with chmod u+x test.sh, the program defined in shebang will then be used (/bin/bash)