Bash script working locally but returning syntax error in CI - bash

On my gitlab CI I am running the following simple script (.gitlab-ci.yml):
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then echo "ok"; fi
Unfortunatley this gives me the error
/bin/sh: eval: line 100: syntax error: unexpected redirection
Running the same command locally as a script is working as expected:
#!/bin/sh
FILE="./file.txt"
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then
echo "ok"
fi
The file has the content:
This has a substring somewhere

/bin/sh is not bash and <<< is a bash extension not available on every shell. Install bash, change shebang to /bin/bash and make sure the script is run under bash or use posix compatible syntax printf "%s\n" "$str" | grep...
Note: UPPER CASE VARIABLES are by convention reserved for exported variables, like IFS COLUMNS PWD UID EUID LINES etc. Use lower case variables in your scripts.

Related

bad substitution bash equivalent in sh

I have this script in Bash :
for conffile in $(ls -1 ${KSCFGISOLINUXFILESLOCATION}); do
for variable in CFG_CRYPT CFG_DISK CFG_DNS CFG_DOMAIN CFG_GATEWAY CFG_HOSTNAME CFG_IFACE CFG_IP CFG_NETMASK CFG_USERKEY CFG_ACTION CFG_TITLE CFG_INITURL CFG_OSVERS CFG_ISOBASE CFG_ISOPATH CFG_ISOPATH_SIMPLE SHI_BOOT_URL DEBUG UUID LOGDIR WWWDIR OSINITDIR WKGDIR; do
SUBSTITUTIONCOUNT=$( grep -c "%%${variable}%%" ${conffile} | cut -f1 -d" " )
if [ ${SUBSTITUTIONCOUNT} -gt 0 ]; then
echo "Modifying ${variable} in ${SUBSTITUTIONCOUNT} places"
echo "Updating ${variable} in ${conffile} to ${-variable}"
sed -i "s^%%${variable}%%^${-variable}^g" ${conffile} 2>&1
fi
done
done
and when I ran it in a sh shell it throws the following error:
/tmp/wf_script-JI1La1/rhel84isoinstall.sh: 158: /tmp/wf_script-JI1La1/rhel84isoinstall.sh: Bad substitution
The problem is that I'm trying to use a bash command in a sh shell
What would be the equivalent of ${!variable} in sh ?
I tried ${-variable} but it was a bad command.
Sh and Dash don't support this feature.
Make sure the script always runs with bash by seeing the shebang correctly and avoiding sh myscript (see Why does my bash code fail when I run it with sh?)
Alternatively, rewrite it to sh using eval, but be careful about code injection:
#!/bin/sh
name=PATH
eval "echo \"\$$name\""

How to check the current shell and change it to bash via script?

#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
fi
The above is part of my script. When the current shell is /bin/csh my script fails with the following error:
If: Expression Syntax
Then: Command not found
If I run bash and then run my script, it runs fine(as expected).
So the question is: If there is any way that myscript can change the current shell and then interpretate rest of the code.
PS: If i keep bash in my script, it changes the current shell and rest of the code in script doesn't get executed.
The other replies are correct, however, to answer your question, this should do the trick:
[[ $(basename $SHELL) = 'bash' ]] || exec /bin/bash
The exec builtin replaces the current shell with the given command (in this case, /bin/bash).
You can use SHEBANG(#!) to overcome your issue.
In your code you are already using she-bang but make sure it is first and foremost line.
$ cat test.sh
#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
else
echo "No File"
fi
$ ./test.sh
readexportfile does not exist
$ echo $SHELL
/bin/tcsh
In the above code even though I am using CSH that code executed as we mentioned shebang in the code. In case if there is no shebang then it will take the help of shell in which you are already logged in.
In you case you also check the location of bash interpreter using
$ which bash
or
$ cat /etc/shells |grep bash

Bash script how to execute a command from a variable

I am trying to alter the Bash function below to execute each command argument. But when I run this script, the first echo works as intended, but the second echo that attempts to append to the scratch.txt file does not actually execute. It just gets echo'd into the prompt.
#!/bin/sh
clear
function each(){
while read line; do
for cmd in "$#"; do
cmd=${cmd//%/$line}
printf "%s\n" "$cmd"
$cmd
done
done
}
# pipe in the text file and run both commands
# on each line of the file
cat scratch.txt | each 'echo %' 'echo -e "%" >> "scratch.txt"'
exit 0
How do I get the $cmd variable to execute as a command?
I found the original code from answer 2 here:
Running multiple commands with xargs
You want eval. It's evil. Or at least, dangerous. Read all about it at BashFAQ #48.

Shell script works in bash but not in ksh

I need to write a script to test if the command blablabla exists in the classpath. So I wrote the following code:
if ! hash blablabla >/dev/null 2>&1; then
echo not found
fi
This works fine when the script is executed in the bash. But if I try it in KSH, then it doesn't work:
#! /usr/bin/ksh
if ! hash blablabla >/dev/null 2>&1; then
echo not found
fi
I expect the echo not found to be executed but instead I get nothing. What's the problem?
I believe command is portable (if that matters):
command -v -- some_command >/dev/null 2>&1 ||
printf '%s\n' "not found"
In bash hash is a builtin command. In ksh it's an alias; aliases aren't active in shell scripts.
alias hash='alias -t --'
Try the which command, which is an external command and therefore shell-independent:
if ! which -s blablabla; then
echo not found >&2
fi
Tha hash command is a shell built-in command in bash, but not in ksh. You might want to use whence instead.
if ! whence blah; then print urgh; fi

bash for loop work in command line, but failed in script

When a run a for statement in debian bash command line, it works fine.
But when I run it in a sh script or run it with bash command, it's keeping report "error near unexpected token `do'"
Where is the difference?
[leon#www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon#www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'
BTW, all works fine in centos enviorment.
Use the -c option so that bash reads the commands from the string you pass in. Also, use single quotes around the command.
bash -c 'for i in {1..10}; do echo $i; done'
your bash command line ends with the first ;
so it gets executed separately as:
bash for i in {1..10};
do echo $i;
done
and man bash says command argument should be a file to load: bash [options] [file]
You can wrap all your script inside inverted commas or in a file. Because here, you're doing bash for i in {1..10} then do echo $i and so on. You should use -c option if you don't put it in a file.

Resources