Aix command help in Echo - shell

QQ can some one help
[db2image#nzakuap047wbcx2{p1tudb047}]:/opt/db2/db2image > db2 -ec list tablespaces | grep -x 0 | wc -l
1
a=`db2 -ec list tablespaces | grep -x 0 | wc -l`
[db2image#nzakuap047wbcx2{p1tudb047}]:/opt/db2/db2image > echo $a
0
Why is that i am not getting 1 for echo $a ??
Thanks

You get result 1 because that is the result of db2 -ec list tablespaces | grep -x 0 | wc -l which will be assigned to a variable.
You need to create an alias if you want to reuse the command.
alias a=`db2 -ec list tablespaces | grep -x 0 | wc -l`

Related

ps -ef | grep * command not working properly

I have simple unix shell script as follows. Which is giving different count for a service
#!/bin/bash
service=$1
ps -ef | grep $service | grep -v "grep" | wc -l
PROCESS_NUM=$(ps -ef | grep $service | grep -v "grep"| wc -l)
echo $PROCESS_NUM
In the above code below line gives output of 2.
ps -ef | grep $service | grep -v "grep" | wc -l
But when same line is assigned to variable as code below its giving output as 3.
PROCESS_NUM=$(ps -ef | grep $service | grep -v "grep"| wc -l)
echo $PROCESS_NUM
Why this is getting increased by 1 and how to tackle it.
You can see what is happening if the script tees the output to a file before counting the lines and then displaying the output after:
#!/bin/bash
service=$1
echo Directly in Script:
ps -ef | grep $service | grep -v grep | tee test.txt | wc -l
cat test.txt
echo Inside Subshell:
RESULT=$(ps -ef | grep $service | grep -v grep | tee test.txt | wc -l)
echo $RESULT
cat test.txt
When the output of a command is captured, bash starts another shell to run the command - but that subshell also shows up in the process list.
When I run that script I get:
$ ./test.sh lca
Directly in Script:
2
gcti 4268 1 0 2018 ? 21:59:03 ./lca 4999
t816826 9159 7009 0 09:22 pts/1 00:00:00 /bin/bash ./test.sh lca
Inside Subshell:
3
gcti 4268 1 0 2018 ? 21:59:03 ./lca 4999
t816826 9159 7009 0 09:22 pts/1 00:00:00 /bin/bash ./test.sh lca
t816826 9166 9159 0 09:22 pts/1 00:00:00 /bin/bash ./test.sh lca

Pipe commands at each xargs pass

Lets say I have this :
echo '/dev/sd'{a..d} | xargs -n 1 lsblk $1
But now I want to pipe each pass of the xargs as for instance :
echo '/dev/sd'{a..b} | xargs -n 1 lsblk $1 | tail -n +2
Now, that does not work obviously, because the pipe is applied to the entire xargs commands. So I might try this :
echo '/dev/sd'{a..b} | xargs -n 1 bash -c "lsblk $1 | tail -n +2"
But the problem is that now $1 has no value inside of the shell.
How can I over come this? Or basically, How can I pipe the execution being done by xargs at each pass?
This should give you the report you are seeking:
find /dev/sd? -print | xargs -n 1 lsblk | egrep '^NAME|^sd'
The output on my system with only sda looks like:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 64G 0 disk

different count using ps and wc to check if service is running in bash

I have a small script to check if a service is running in bash
#!/bin/bash
countlines=""$(ps -ef | grep netdata | grep -v grep | wc -l)
echo $countlines >> mylog.log
if [ ${countlines} -lt 3 ];then
sudo /bin/systemctl restart netdata
fi
The problem is when I issue a ps -ef | grep netdata | grep -v grep | wc -l at the command line the result is always 3 but mylog.log is:
6
[update: added filtered ps -ef results]
forge#reportserver:~ ps -ef | grep netdata
netdata 22308 1 0 08:38 ? 00:00:37 /usr/sbin/netdata -D
netdata 22386 22308 0 08:38 ? 00:00:58 /usr/libexec/netdata/plugins.d/apps.plugin 1
netdata 47045 22308 0 11:38 ? 00:00:02 bash /usr/libexec/netdata/plugins.d/tc-qos-helper.sh 1
forge 52028 27902 0 12:34 pts/8 00:00:00 grep --color=auto netdata
why such a discordance?
split into 2 commands to see why:
ps_grep_output=$(ps -ef | grep netdata | grep -v grep)
echo "$ps_grep_output" >> mylog.log
countlines=$(wc -l <<< "$ps_grep_output")
echo "$countlines" >> mylog.log

How to store a variable with command status [shell script]

I am searching a word in a file through grep command. Now I need to store the status in a variable V1 with 0 or 1. how can i do it?
tail -n 2 test.s | grep -q "FA|"$(date "+%m/%d/%Y")
tail -n 2 test1.s | grep -q "FA|"$(date "+%m/%d/%Y")
tail -n 2 test2.s | grep -q "FA|"$(date "+%m/%d/%Y")
If the above searching word is found then variable V1 value should be 0 else 1.
file content :
keytran|20160111|test.s
submKeyqwqwqw|NDM|Jan 11 01:34|test.s|6666666|sdgdh-RB|ltd.ET.CTS00.act
loadstatus|thunnnB|6666666|FA|01/16/2016|01:34:57|01/16/2016
|01:37:13|load|test.s
please suggest
Depending on your shell, after each command execution the status of the previous command is available in a special variable: bash family $?, csh family $status$:
#/bin/bash
tail -n 2 test.s | grep -q "FA|"$(date "+%m/%d/%Y")
V1=$?
or
#/bin/csh
tail -n 2 test.s | grep -q "FA|"$(date "+%m/%d/%Y")
set V1=$status

BASH: Remove newline for multiple commands

I need some help . I want the result will be
UP:N%:N%
but the current result is
UP:N%
:N%
this is the code.
#!/bin/bash
UP=$(pgrep mysql | wc -l);
if [ "$UP" -ne 1 ];
then
echo -n "DOWN"
else
echo -n "UP:"
fi
df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t && echo -n ":"
top -bn2 | grep "Cpu(s)" | \sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \awk 'END{print 100 - $1"%"}'
You can use command substitution in your first sentence (notice you're creating a subshell in this way):
echo -n $(df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t ):

Resources