3

I want to run source ./script_name instead of ./script_name. Can I create an alias for this ?

The Jenkins job will run the script as ./script_name and I can't edit the way it will run the script on the server. I need to run this script as a source so as to run the commands in the same shell. So whenever the Jenkins runs ./my_script it will in turn run source ./my_script instead.

Z0OM
  • 3,149
  • the given links don't answer specific to this problem. – Anubhav Rai Mar 31 '23 at 09:47
  • 1
    What is the problem? you want to run a script over an alias or not? – Z0OM Mar 31 '23 at 09:52
  • The problem is that I am not able to create alias with ./ thing. – Anubhav Rai Mar 31 '23 at 09:54
  • because the Jenkins is calling like this only and I can't edit it to call the script the other way. – Anubhav Rai Mar 31 '23 at 10:04
  • 2
    can you edit the question and add more details? what's with this : alias script_name="source /home/user/./script_name" in your bashrc ? – Z0OM Mar 31 '23 at 10:10
  • https://stackoverflow.com/questions/21276351/how-can-i-execute-shell-script-in-jenkinsfile – Z0OM Mar 31 '23 at 10:13
  • It should be like alias ./script_name="source /home/user/./script_name" only. – Anubhav Rai Mar 31 '23 at 10:17
  • 4
    No, you can't, sorry. Instead, please clarify why you need this. If you are using Jenkins to run scripts and commands, then include this in the question along with what issue it is that you are trying to solve. Note that creating an alias is not the issue you are trying to solve, it's a solution that you think solves an issue that you haven't mentioned, and that I think you have misunderstood. – Kusalananda Mar 31 '23 at 10:34
  • I edited the question for clarity and grammar. If it changed the intent of the question, then please cancel it. – RonJohn Mar 31 '23 at 11:04
  • 1
    @RonJohn edited the question – Anubhav Rai Mar 31 '23 at 11:34
  • What does "restricted permissions" have to do with anything at all? No, you cannot -- full stop impossible -- make a shell you can't configure retroactively decide to source your script instead of run it, if the only thing you can configure is the text of that script, because by the time the script is running it was already forked into a subprocess. But the statement above is making a bunch of caveats the question currently doesn't support; you don't say anything about whether "if the only thing you can configure is the text of the script" is true, f/e. – Charles Duffy Mar 31 '23 at 21:27
  • There may be some moral equivalent way to accomplish your larger goal, but if you don't tell us that larger goal then we can't help you, and we're going to be over here being very annoyed that you're not helping us help you by describing the details of which restrictions you do and don't have and what the surrounding context is. – Charles Duffy Mar 31 '23 at 21:29
  • (For example, in a lot of circumstances it might be possible to make the sourced-vs-executed question completely moot by having the forked-off subprocess take over whatever work was previously going to be done later in the sourced script so the work is instead done in the forked-off subprocess; but we need to know details to work out whether your circumstances in particular fall into that category). – Charles Duffy Mar 31 '23 at 21:30

1 Answers1

3

You have a bash script in /home/user/ and the bash script is myscript you can set an alias to run

/home/user/myscript

#!/bin/bash
echo "WELCOME BASH"

Run chmod +x /home/user/myscript

  1. create an alias and execute by alias name

    • Temporary alias — run this in the terminal

      alias myscript="/home/user/myscript"

    • Permanent alias in ~/.bashrc (/home/user/.bashrc) — add/edit this to your .bashrc file

      alias myscript="/home/user/myscript"

    Now run in terminal: myscript

Option 1

Add this to your bashrc:

alias myscript="source /home/user/./myscript" or alias my_script="source ./my_script"

Run my_script, to run source ./my_script

This work only for your user account. If you want the alias for all users, you will need to add the alias to the system-wide shell configuration file /etc/bash.bashrc for bash or ask the administrator to add it.

Option 2

Login to your jenkins server and navigate to the job configuration page for the job you want to use the source command

In build section of the job configuration, add a new build step of type Execute shell

In Command field, define a function that wraps the source command

Example:

function my_source() {
    source ./home/user/my_script
    # or
    source /home/user/./myscript
    # or
    source /home/user/myscript
}

Assign this function to an alias temporary or permanent alias my_alias='my_source'

Option 3

Create a wrapper script

#!/bin/bash
source /home/user/my_script

Make the file executable

chmod +x my_script

now you can run ./my_script

Check if the script is located in the same directory as the original script, or in directory that is included in your system PATH variable

Option 4

when the Jenkins job tries to run ./my_script, it will actually run source ./my_script with this.

Create a symbolic link to your script: ln -s $(realpath my_script) source

When Jenkins job run ./my_script, it will run source ./my_script from the symbolic link you created

Use this with caution, clean up the symbolic link after you done the job with

rm source

Sources:

Z0OM
  • 3,149
  • 3
    @AnubhavRai Can you add some more details why you can't edit the server or what are your restrictions, because i am using jenkins too in a docker container and i got not problems to create and execute jobs with script or other ... – Z0OM Mar 31 '23 at 11:44
  • I don't have permissions to do that. – Anubhav Rai Mar 31 '23 at 11:59