shell grep command from the output of wget - grep

I have a script to check the status of a url. I am trying to grep for the word connected from the below mentioned wget command output and print Running if it is found else print Not Running. How can I modify my grep to print only the word without the all the output from wget command
#!/bin/ksh
STAT=`wget 'http://server:port/ABC_Service/app' | grep connected`
if [ -z "$STAT" ] ; then
echo "Running"
else
echo "Not Running"
fi
Output of wget command:
--2013-05-31 11:09:32-- http://server:port/ABC_Service/app
Resolving server... 10.109.136.31
Connecting to server|10.109.136.31|:port... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Authorization failed.
Desired Output from my script:
Running

Check the return code
#!/bin/ksh
#use your wget command in place of echo below
echo "connected" 2>&1 | grep connected >/dev/null
retcode=$?
if [ $retcode = 0 ]
then
echo "Running"
else
echo "Not Running"
fi

If you can use Bash, then the variable $? reads the exit status of the last command executed.

Related

how to trigger and monitor Jenkins job from CI and getting results after is finished

I am trying to implement a script in gitlab CI to trigger a smoke test via Jenkins and then get the results.
So far I am able to trigger the job successfully and I am trying to follow this to implement a monitoring stage and then get the result once the job finishes.
my issue is that I implemented a while loop to monitor if the Jenkins job has finished so far the script is giving either syntax errors (when copied in gitlab) or if run in the terminal I get:
job is building? true
waiting...
parse error: Invalid numeric literal at line 2, column 0
job is still building?
job is building?
This is what I am using so far:
#!/bin/bash
running="true"
while [ "$running" != "false" ]
do
echo "job is building? ${running}"
echo "waiting...";
sleep 2;
running=$(curl -s --user ${EMAIL}:${TOKEN} ${URL}/${var}/lastBuild/api/json | jq .'building')
echo "job is still building? ${running}"
done
echo "Done!"
buildNumber=$(curl -s --user $EMAIL:$TOKEN ${URL}/$ENV-${var}/lastBuild/api/json | jq ".url" | awk -F "/" '{print $(NF-1)}')
echo "getting results for build ${buildNumber}"
curl -s --user ${EMAIL}:${TOKEN} ${URL}/${ENV}-${var}/lastBuild/api/json | jq ".url" | awk -F "/" '{print $(NF-1)}'
curl -v --silent --user ${EMAIL}:${TOKEN} ${URL}/${ENV}-${var}/lastBuild/consoleText 2>&1 | grep -i "finished:"
UPDATE
the script is running now ok in my local terminal
the change was
running="true"
while [ "$running" != "false" ]
do
echo "job is building? ${running}"
echo "waiting..."
sleep 2
curl -s --user $EMAIL:$TOKEN $URL/$ENV-${var}/lastBuild/api/json --output now.txt
running=$(jq .'building' now.txt)
echo "job is still building? ${running}"
done
The problem is still on Gitlab CI as after copy pasting this script I get in the pipeline
/bin/sh: eval: line 149: syntax error: unexpected "done"
I'm guessing that the error
parse error: Invalid numeric literal at line 2, column 0
comes from the line that does the curl
running=$(curl -s --user ${EMAIL}:${TOKEN} ${URL}/${var}/lastBuild/api/json | jq .'building')`
I'm assuming that the error is thrown by jq when it tries to parse the json and fails. Since this line has an error, the variable running never gets properly updated, meaning that the rest of the script doesn't work as intended.
If you fix this line, the rest of your pipeline should work. Consider looking into this question which has a similar problem and some solutions.

SSH into machine, check to see if file exists, then fail Jenkins Execute shell if it does

I am trying to SSH into a machine from a Jenkins Execute shell, check to see if a file exists on the machine and if it does then fail the shell. I have the following code however I can seem to get Jenkins to recognize that the output of echo is "yes" or no"
Please let me know what you think...
sshpass -p ${ServerNodePw} ssh -T -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${ServerNodeUser}#${ServerNodeIP} << EOSSH
(ls /Volumes/ServerHD/Users/username/Desktop/JenkinsVMbuild.lock && echo yes) || echo no
2>/dev/null 1>/dev/null
echo "$?"
if [ "$?" = yes ]
then
echo "File found"
exit 1
currentBuild.result = 'FAILURE'
else
echo "File not found"
fi
EOSSH
The value of $? is the exit status of the last executed command (or pipeline). In your case, it would always expand to 0.
You can use command substitution instead if you want to store the echoed string in a variable.
You will also need to properly escape (or quote) the script in your here document. Otherwise $var will be substituted by the caller before passing the script to ssh.

Why is wc -l returning 0 in a sh step subshell in Jenkins/groovy

I have a Jenkins script that looks like this
stage ("Build and Deploy") {
steps {
script {
def statusCode = sh(script:"""ssh ${env.SERVER_NAME} << EOF
cd ${env.LOCATION}
git clone -b ${env.GIT_BRANCH} ${env.GIT_URL} ${env.FOLDER}
cd ${env.FOLDER}
... some other stuff goes here but isnt relevant ..
sudo docker-compose up -d --build
if [ ! \$(sudo docker container ls -f "name=config-provider-*" | wc -l ) -eq 4 ]
then
exit 1
fi
""", returnStatus:true).toString().trim()
if (statusCode == "1") {
error("At least one container failed to start")
}
}
}
}
What I want is to exit with error code 1 in the script if the number of running containers is not equal to 3 (wc -l == 4 including the header), but the if statement is evaluating true and exiting with error code 1 even though i know that the containers are successfully running.
I have tried
echo sh(script: """ssh ${env.SERVER_NAME} << EOF
echo \$(sudo docker container ls -f "name=config-provider-*" | wc -l)
""", returnStdout:true).toString()
and
echo sh(script: """ssh ${env.SERVER_NAME} << EOF
echo \$(sudo docker container ls -f "name=config-provider-*")
""", returnStdout:true).toString()
The latter outputted 4 lines within jenkins showing all of the running containers, as expected, but the former which includes "| wc -l" returned and printed out 0 in jenkins.
I have reproduced the steps of this script line by line manually from start to finish and it works as intended when not run from within jenkins.
Additionally, manually running the command:
[ ! $(sudo docker container ls -f "name=config-provider-*" | wc -l ) -eq 4 ] && echo failed
echoes nothing, and the following command returns an output of 4, which is expected.
echo $(sudo docker container ls -f "name=config-provider-*" | wc -l )

PJL status readback commands

How to receive respond on Windows from printer after send PJL command?
I tried to spy network printer with WireShark and read printer socket directly but nothing work.
On windows is only possible with network printers with Paraphernalia. There is no bidirectional printer drivers for common models. So i have installed linux on virtualmachine.
I used the bidirectional USB driver on linux (the same drivers is on mint, *buntu distro) and execeute a pjl command from cmd.txt with this bash script below.
But i still cannot recognize when to read PJL Respond. Using fuser command to recognize /dev/usb/lp0 busy state does not work so i try to read in a loop with dd and check size of readback. It works but sometimes has to send PJL few times before got response.
#!/bin/bash
PRINTER=/dev/usb/lp0
ESCAPE=$(echo -e "\e")
FF=$(echo -e "\x0C")
char=
IFS=""
SSKIP=0
echo "" > a.txt
echo "" > b.txt
echo "" > temp.txt
while read p; do
echo -e "#######BEGIN$p-\r\n" >> a.txt
echo $p
while true
do
#WAIT FOR NOT BUSY
while true
do
fuser -s $PRINTER
if [ $? -ne 0 ]
then
break
fi
done
#send PJL to printer
echo -e "\e%-12345X#PJL\r\n#PJL INQUIRE $p \r\n\e%-12345X" > $PRINTER
#TRY TO READ IMMEDIATELY
char=$(dd if=$PRINTER of=temp.txt 2>&1)
#send escape after read
echo -e "\e" > "$PRINTER"
#CHECK THAT dd READ ANY BYTES
echo "$char" | grep "bytes copied" > /dev/null
if [ $? -ne 0 ]
then
printf "."
else
echo "$char" | grep "^0 bytes copied" > /dev/null
if [ $? -ne 0 ]
then
cat temp.txt >> b.txt
echo "" > temp.txt
break
else
printf "."
fi
fi
char=""
done
echo
echo -e "#######END$p-\r\n" >> a.txt
done <cmd.txt

Error running egrep on Solaris

Sun OS 5.8
Bash shell script
Oracle 10g database
Error 1 the command executing at the time of the error was egrep ORA-\|TNS-\|PLS-\|Error\|PLW-\|IMP-\|EXP-\|RMAN-\|SQL- alert_work.log > alert.err on line 11
The "egrep" line runs successfully when I run it manually. But in a bash script (cron job) it gets the above error. Here is the script:
#!/bin/bash
SID=$ORACLE_SID
DOMAIN=$(uname -n)
DBALIST='dbak#xxx.com'
YESTERDAY=`TZ=CST+24 date +%Y-%m-%d`
cd $ORACLE_HOME/admin/$SID/bdump
mv alert_${SID}.log alert_work.log
touch alert_${SID}.log
cat alert_work.log >> alert_${SID}.hist
egrep ORA-\|TNS-\|PLS-\|Error\|PLW-\|IMP-\|EXP-\|RMAN-\|SQL- alert_work.log > alert.err
if [ `cat alert.err|wc -l` -gt 0 ]
then
mailx -s "${DOMAIN}.${SID} ALERT LOG ERRORS FOUND" $DBALIST < alert.err.log
fi
/usr/bin/mv alert_work.log $ORACLE_HOME/admin/$SID/bdump/hist/alert_${SID}_${YESTERDAY}.log
exit
I am suspicious of your egrep regular expression, The fact that you have not quoted it, and create a script from within a Bash script leads and then run the script, leads me to think that you will end up with:
egrep ORA-|TNS-|PLS-|Error|PLW-|IMP-|EXP-|RMAN-|SQL- alert_work.log > alert.err
which is not what you intended. Try:
egrep 'ORA-\|TNS-\|PLS-\|Error\|PLW-\|IMP-\|EXP-\|RMAN-\|SQL-' alert_work.log > alert.err
That should preserve the back slashes.

Resources