Unable to run DSJOB command from script but the same command is running in command line - shell

I am trying to run the command mention below to get the status of the DataStage job in a script,
JOBSTATUS = $(dsjob -jobinfo "$Project_Name" "$Job_Name" | head -l | cut -d"(" -f2 | cut -d")" -f1)
echo $JOBSTATUS
I tried using (Tilde)
JOBSTATUS = `dsjob -jobinfo "$Project_Name" "$Job_Name" | head -l | cut -d"(" -f2 | cut -d")" -f1`
echo $JOBSTATUS
But still getting the error:
JOBSTATUS: not found [no such file or directory]
At the same time if try to run this command in command line, it is running fine and giving the output.
I am new to Unix, am I missing anything?

Related

journalctl --after-cursor is not working well with shell script

I am trying to get the logs from journalctl after a specified time with the use of cursor option.
Below is code in script file.
value=$( journalctl -n 0 --show-cursor | tail -1 | cut -f2- -d: | sed 's/ /"/;s/$/"/')
echo "$value"
sleep 20
echo "journalctl --after-cursor=$value"
journalctl --after-cursor=$value
The ouput of this script file is
"s=3057f92d5b3e4afdb5eb91c22e880074;i=1f646;b=0bc7e6e9f16f4847b2d50e0a0dd31023;m=a10d4c4d1;
t=5bba8ac2477ae;x=1cc1645fed6ffc79"
journalctl --after-cursor="s=3057f92d5b3e4afdb5eb91c22e880074;i=1f646;
b=0bc7e6e9f16f4847b2d50e0a0dd31023;m=a10d4c4d1;t=5bba8ac2477ae;x=1cc1645fed6ffc79"
Failed to seek to cursor: Invalid argument
As we can see above the journalctl --after-cursor results in "Failed to seek cursor error".
However if the same is executed in command line terminal, the --after-cursor gives the output.
Is there something needed to be done before calling journalctl with after-cursor option in shell script?
Bash is very finicky about interpolation. You need to trim the double-quotes around $value and then quote it later on:
value=$( journalctl -n 0 --show-cursor | tail -1 | cut -f2- -d: | sed 's/ /"/;s/$/"/') | tr -d '"')
echo "$value"
sleep 20
echo "journalctl --after-cursor=$value"
journalctl --after-cursor="$value"
I've confirmed that this works by running the above and I've also used this same method on another script which uses cursors.

How to output bash command to stdout and pipe to another command at the same time?

I'm working on a server and to show detailed GPU information I use these commands:
nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`
However as you can see, nvidia-smi is called twice. How can I make the output of nvidia-smi go to output and pipe to another command at the same time?
Use tee:
ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`
Since stdout is piped, you can't make a copy to it, so I picked stderr to show output.
If /dev/stderr is not available, use /proc/self/fd/2.

Bash command output as variable returning /bin/bash

When I run the following command in my terminal I get the expected value :
delayedjobs="$(ps aux | grep delayed_job | grep -v grep | awk '{print $11}' | awk 'END{print}' | cut -d "." -f2)" && echo $delayedjobs
However, when I run the following script, the variable returns as "/bin/bash"
#!/bin/bash
delayedjobs="$(ps aux | grep delayed_job | grep -v grep | awk '{print $11}' | awk 'END{print}' | cut -d "." -f2)"
echo ${delayedjobs}
root#central:/home/tblake# ./myscript.sh
/bin/bash
Can anyone explain this? Im having a heck of a time figuring this out.
Has you named your skript delayed_job ? Your script detect his own process.

Bash script echos but won't execute

I'm creating a simple bash script to remove all Bluetooth devices from my system. What it does is use bt-device -l to list the bluetooth devices, then grabs the MAC address in between the parens, then calls bt-device -r <MACAddress> to remove the device. I'm not that great at bash scripts but when I replace the bt-device call with echo, I get the correct output. When I put the command back in it says the device wasn't found. If I manually make the call it works (outputs "Done").
Sample output of the bt-device -l command:
Added Devices:
Samico BP (12:34:56:78:9a:bc)
SensorTag 2.0 (12:34:56:78:9a:bd)
And the script I'm using:
#!/usr/bin/env bash
bt-device -l | sed 1d |
while read x; do
bt-device -r $x | cut -d "(" -f2 | cut -d ")" -f1
done
When I run it, it runs the bt-device -r command but the output is Error: Device not found as if I'd typed the MAC address wrong. If I replace the bt-device call in the script with echo, I get the list of MAC addresses as expected.
Updated script working
#!/usr/bin/env bash
bt-device -l | sed 1d |
while read x; do
bt-device -r $(echo $x | cut -d "(" -f2 | cut -d ")" -f1)
done
The problem is that you're running cut -d "(" -f2 | cut -d ")" -f1 on the output of bt-device -r, instead of on its argument.
Instead, you should write something like this:
bt-device -l | sed 1d | cut -d "(" -f2 | cut -d ")" -f1 |
while read x; do
bt-device -r $x
done
You'd want to process $x prior to using it:
bt-device -l | sed 1d |
while read x; do
dev=$( echo "$x" | cut -d "(" -f2 | cut -d ")" -f1 )
bt-device -r "$dev"
done

Something is wrong with unix script

Following is my script, every time I run this it goes into else part. when I run the TEST2EVAL command it gives me 1
#!/bin/sh
TEST2EVAL='ps auxf | grep some.jar | grep -v grep | wc -l'
if [ "$TEST2EVAL" = 1 ]
then
java -jar /path/to/jar &
else
echo "Running"
fi
Assuming you are trying to find out if any processes are running with some.jar on their command lines you probably want:
if pgrep -f some.jar; then
echo running;
else
echo not running;
fi
In in order save the output of a command in a variable, you have to enclose the command in backticks (`), not single quotes ('). Thus, change the second line of your script to:
TEST2EVAL=`ps auxf | grep some.jar | grep -v grep | wc -l`
You are using the wrong quotes for command substitution: not single quotes:
TEST2EVAL='ps auxf | grep some.jar | grep -v grep | wc -l'
but backquotes:
TEST2EVAL=`ps auxf | grep some.jar | grep -v grep | wc -l`
Better yet, use TEST2EVAL=$(ps auxf | grep some.jar | grep -v grep | wc -l) instead. It's much clearer, supported by all POSIX-compatible shells, and can be nested more easily when necessary.

Resources