<p>For executing the command in the Jenkins Freestyle Job.</p>

<pre><code>#!/bin/bash

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done
</code></pre>

<p>For ssh</p>

<pre><code>ssh -o StrictHostKeyChecking=no user@host '
   command_1 to execute
   command_2 to execute
   ./script.sh to execute
   command_3 to execute
'
</code></pre>

CC BY-SA 4.0
2019-12-18T09:18:33.587Z
Is there an option with the 'Jenkins Freestyle Job' to add ssh to a remote machine and then run the logic?
arielma
CC BY-SA 4.0
2019-12-20T04:42:50.643Z
Let me try to understand the question you are talking about.
You want to install ssh into some instance and then execute commands?
Manish_
CC BY-SA 4.0
2019-12-18T08:44:02.890Z
2019-12-18T08:44:02.890Z
Manish_
<p>You can specify the script in the "execute shell" step of the build. <em>Where</em> it gets executed will depend on what labels the job needs, and which nodes those labels are associated with.</p>

<p>It sounds like you want to execute this script on an agent. As long as the agent is available, you can request it via a label in the job configuration. It's a bit difficult to determine this without having access to your setup, but if <em>e.g.</em> you have a set of agents on Debian boxes, you could attach the label <code>debian</code> to them, and specify that the job should run on <code>debian</code> nodes in the job configuration. </p>

<p>If on the other hand it should be run on <code>master</code> (where Jenkins itself is running), you can specify the label <code>master</code>.</p>

<p>You thus need to put your script into the "Execute Shell" box that Jenkins provides you, perhaps declaring what shell and options you want to use:</p>

<pre><code>/bin/bash -xe
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done
</code></pre>

<blockquote>
  <p>I need also to add an ssh command to the environment. This should be executed using passwordless authentication.</p>
</blockquote>

<p>For this to happen, you need ssh keys to be exchanged with the host onto which you want to ssh into.</p>

CC BY-SA 4.0
2019-12-18T05:12:39.417Z
do you mind put my syntax example on top of my do I'll understand how it should look?
arielma
CC BY-SA 4.0
2019-12-18T07:45:54.143Z
I've added your script verbatim. Does this help you answer the question?
Bruce Becker
CC BY-SA 4.0
2019-12-18T09:07:44.993Z
and I want to add ssh to some remote machine? for example: ssh -o StrictHostKeyChecking=no -n {user}@{bitbucket_server}
arielma
CC BY-SA 4.0
2019-12-17T18:15:11.787Z
2019-12-18T07:45:13.100Z
2019-12-18T07:45:13.100Z
Bruce Becker
<p>I have the below shell script:</p>

<pre><code>du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done
</code></pre>

<p>How can I run it thought Execute shell step in a Jenkins freestyle job? 
<a href="https://i.sstatic.net/OcGHZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OcGHZ.png" alt="Execute shell"></a></p>

<p>I need also to add an ssh command to the environment. This should be executed using passwordless authentication.</p>

<p>Note: I don't want to connect to the environmnet and run this shell, but want to execute it directly from the box.</p>

CC BY-SA 4.0
2019-12-17T17:36:30.663Z
The screenshot seems to show that you are using a freestyle job, not a pipeline job. Is this the case?
Bruce Becker
CC BY-SA 4.0
2019-12-17T17:59:48.577Z
You are correct
arielma
CC BY-SA 4.0
2019-12-17T18:08:37.817Z
I'm a bit confused by your note regarding SSH. Are you saying that you want the script to be executed over SSH on an agent?
Bruce Becker
CC BY-SA 4.0
2019-12-17T18:37:00.883Z
Yes. On bitbucket remote server
arielma
<jenkins-pipeline><shell-script>
How to add shell script to jenkins freestyle job
arielma
# How to add shell script to jenkins freestyle job

(Asked by: username_0 on 2019-12-17)

I have the below shell script:
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done

How can I run it thought Execute shell step in a Jenkins freestyle job? 

I need also to add an ssh command to the environment. This should be executed using passwordless authentication.
Note: I don't want to connect to the environmnet and run this shell, but want to execute it directly from the box.

**Tags:** jenkins-pipeline, shell-script

> **Comments:**
> **username_1**: The screenshot seems to show that you are using a freestyle job, not a pipeline job. Is this the case?  
> **username_0**: You are correct  
> **username_1**: I'm a bit confused by your note regarding SSH. Are you saying that you want the script to be executed over SSH on an agent?  
> **username_0**: Yes. On bitbucket remote server  

---

## 2 answers

### Answer by username_2 on 2019-12-18. Score: 1 | Accepted answer

For executing the command in the Jenkins Freestyle Job.
#!/bin/bash

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done

For ssh
ssh -o StrictHostKeyChecking=no user@host '
   command_1 to execute
   command_2 to execute
   ./script.sh to execute
   command_3 to execute
'

> **Comments:**
> **username_0**: Is there an option with the 'Jenkins Freestyle Job' to add ssh to a remote machine and then run the logic?  
> **username_2**: Let me try to understand the question you are talking about. You want to install ssh into some instance and then execute commands?  

---

### Answer by username_1 on 2019-12-17. Score: 1

You can specify the script in the "execute shell" step of the build. Where it gets executed will depend on what labels the job needs, and which nodes those labels are associated with.
It sounds like you want to execute this script on an agent. As long as the agent is available, you can request it via a label in the job configuration. It's a bit difficult to determine this without having access to your setup, but if e.g. you have a set of agents on Debian boxes, you could attach the label debian to them, and specify that the job should run on debian nodes in the job configuration. 
If on the other hand it should be run on master (where Jenkins itself is running), you can specify the label master.
You thus need to put your script into the "Execute Shell" box that Jenkins provides you, perhaps declaring what shell and options you want to use:
/bin/bash -xe
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done


I need also to add an ssh command to the environment. This should be executed using passwordless authentication.

For this to happen, you need ssh keys to be exchanged with the host onto which you want to ssh into.

> **Comments:**
> **username_0**: do you mind put my syntax example on top of my do I'll understand how it should look?  
> **username_1**: I've added your script verbatim. Does this help you answer the question?  
> **username_0**: and I want to add ssh to some remote machine? for example: ssh -o StrictHostKeyChecking=no -n {user}@{bitbucket_server}  

---

