I have scriptlets written using the bash shell instead of the sh shell. During the build process I have warnings that indicate that I should be using sh instead of bash. Is there a parameter I can set to indicate that I am using bash instead of sh shell??
The rpm seems to install ok however but I wanted to know if I could specify bash somewhere?
thanks
Have you tried including a sh-bang line as the first line in the scriptlets in your spec file (like I just noticed the comment mentioned)
%post
#!/bin/bash
... rest of our post script here
If this works, you should also set up a scriptlet dependency like
Require(post): bash
Related
How can I make Jenkins use Bash rather than its native shell for just one Jenkins pipeline/Jenkinsfile? Does the "agent" help me to do this?
I wrote a shell script for deployment but some of the parameters contain whitespace which messes up the resulting command I generate by losing some args. I've found how to avoid this problem by globally configuring Jenkins shell type to be Bash. But when I change the global shell type, my other Jenkins pipelines that use the Jenkins docker plugin syntax get broken when they use the 'sh' command within a docker container. My workaround is to ping pong the global setting for shell type depending on which Jenkins build I want to run. Its a royal PITA.
I'm embarrassed to say all I needed was a shebang.
My Jenkinsfile runs a custom (bash) shell script, using Jenkin's sh command, like in the following:
sh "./deploy.sh \"arg1\" \"arg 2\" \"arg3\""
In order to force deploy.sh to run within Bash, the contents of deploy.sh need to include #!/bin/bash on the first line, as in:
#!/bin/bash
echo "deploy args: $#"
Regardless, I think there should be a way to tell a Jenkins pipeline that it should run with specific settings, like sh=bash.
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 5 years ago.
I have a file to be sourced in Centos 7.
It just works fine if I do :
$ source set_puregev_env
however, if I put this in a shell script, it doesn't work..
$ sh xRUN
xRUN: line 3: source: set_puregev_env: file not found
this is my shell script : xRUN
#!/bin/bash
source set_puregev_env
can anyone tell me what I might be doing wrong, or missing?
source is a command implemented in bash, but not in sh.
There are multiple ways to fix your script. Choose either one.
Run the script using bash interpreter
When you are invoking the xRUN script - you are explicitly telling it to be interpreted by sh
$ sh xRUN
To change and interpret the script with bash instead do
$ bash xRUN
This will make bash interpret the source command, and your script will work.
Use dot command to make script bourne compatible
You can also change the source with a dot command which does the same thing but is supported in both bourne and bash.
Change the line:
source set_puregev_env
With:
. set_puregev_env
Now the script will work with either sh or bash.
Make script executable
You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this:
$ ./xRUN
It will then use the command specified in the shebang and use the rest of the script as input. In your case it will use bash - since that is specified in the shebang.
im trying to get my server to execute a simple bash script:
#!/bin/bash
echo Hello World
After saving this to /var/www/script (im saving it to the web directory for no reason in particular) i try and execute it with
exec /var/www/script
This fails returning i don't have permission to execute it, sudo exec isn't a thing so i do sudo -i then run exec /var/www/script as root and i still have permission denied. I fairly uncertain why executing it as root doesn't work. Im wondering if i'm
A) using the wrong command to execute a bash script
B) have incorrect formatting in the script
C) shouldn't have saved it to /var/www/
D) done some other thing that i'm not even aware of.
Im running ubuntu server 16.04 if that helps.
File Permissions
First, make sure that you have the correct file permissions:
chmod +x /var/www/script_name #Gives the current user execute permissions
Executing Your Bash Script
In order to execute your bash script, the easiest option is to just simply call it (without any additional commands) by typing in the relative path to the script:
/var/www/script_name
There are other options for explicitly executing your script from the shell (in your case, use the bash shell to execute your script as a bash script). From TLDP documentation...
A script can also explicitly be executed by a given shell, but generally we only do this if we want to obtain special behavior, such as checking if the script works with another shell or printing traces for debugging:
rbash script_name.sh # Execute using the restricted bash shell
sh script_name.sh # Execute using the sh shell
bash -x script_name.sh # Execute using the bash shell
A Note on File Extensions: "Shebang" line > File extension
It is not an advised practice to use file extensions with your scripts, especially if you think your code may evolve beyond its current functionality.
Just in case you were wondering if the file extension may be your problem... it is not. It is important that you know that the file extension of a script isn't necessary at all. What matter is what you put in the "shebang" line:
To use the sh shell:
#!/bin/sh
To use the bash shell:
#!/bin/bash
It won't matter what file extension you use - the "shebang" line indicates what shell will be used to execute the script. You could save a script with the "shebang" of #!/bin/bash as script_name.py, but it would remain a bash script. If you attempt to execute it, ./script_name.py, it would be executed as a bash script.
As #Arjan mentioned in the comments, using file extensions for your script could lead to unnecessary complications if you decide to change the implementation of your project (i.e., a different shell / language):
I could decide later to shift my project to sh, python, perl, C, etc. Perhaps because I want to add functionality. Perhaps because I want to make it portable to a system without bash. It would be much more difficult if I used the .sh file extension, since then I'd need to change all my references to the script just because I changed its implementation.
You have two choices:
Run it as an argument to bash:
bash /var/www/script
Alternatively, set the execute bit:
chmod +x /var/www/script
And, now you can execute it directly:
/var/www/script
I am trying to store the content of a text file in one variable and using that variable as body in email sending. Below is the code to store the value in variable
ct=`cat a.txt`
I tried with
ct="`cat a.txt`"
The issue is, when I am executing the script as sh script.sh. The whole contents of a.txt are getting stored in variable ct.
But when I am executing the script without the sh extension (i.e. script.ksh). Only few contents are storing is variable ct. I am not able to trace what the issue is.
Need your help.
On default Linux systems the /bin/ksh is not installed. Sometimes you can install a ksh package. The work-around ln -s /bin/bash /bin/ksh is dangereous, since bash is a superset most of the times, but is not always compatible.
For the CentOS environment the best way would be shanging the shebang line to #!/bin/bash, but be aware of the following issues:
Version control
When you want to use the same scripts on CentOS and other Unix OS, you need to maintain two versions or get bash installed on the other Unix OS.
Developers should write in one language, bash or ksh
When you need to maintain large ksh scripts, writing new scripts in bash will become confusing.
Check/test your code
Do you set variables inside while-loops? They can get lost!
I'm a newbie to scripting languages trying to learn bash programming.
I have very basic question. Suppose I want to create three folders like $HOME/folder/
with two child folders folder1 and folder2.
If I execute command in shell like
mkdir -p $HOME/folder/{folder1,folder2}
folder will be created along with child folder.
If the same thing is executed through script I'm not able get expected result. If sample.sh contains
#!/bin/sh
mkdir -p $HOME/folder/{folder1,folder2}
and I execute sh ./sample.sh, the first folder will be created then in that a single {folder1,folder2} directory is created. The separate child folders are not created.
My query is
How the script file works when we compared to as terminal command? i.e., why is it not the same?
How to make it work?
bash behaves differently when invoked as sh, to more closely mimic the POSIX standard. One of the things that changes is that brace expansion (which is absent from POSIX) is no longer recognized. You have several options:
Run your script using bash ./sample.sh. This ignores the hashbang and explicitly uses bash to run the script.
Change the hashbang to read #!/bin/bash, which allows you to run the script by itself (assuming you set its execute bit with chmod +x sample.sh).
Note that running it as sh ./sample.sh would still fail, since the hashbang is only used when running the file itself as the executable.
Don't use brace expansion in your script. You could still use as a longer method for avoiding duplicate code:
for d in folder1 folder2; do
mkdir -p "$HOME/folder/$d"
done
Brace expansion doesn't happen in sh.
In sh:
$ echo {1,2}
produces
{1,2}
In bash:
$ echo {1,2}
produces
1 2
Execute your script using bash instead of using sh and you should see expected results.
This is probably happening because while your tags indicate you think you are using Bash, you may not be. This is because of the very first line:
#/bin/sh
That says "use the system default shell." That may not be bash. Try this instead:
#!/usr/bin/env bash
Oh, and note that you were missing the ! after #. I'm not sure if that's just a copy-paste error here, but you need the !.