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

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

Related

Prevent grep from exiting in case of nomatch

Prevent grep returning an error when input doesn't match. I would like it to keep running and not exit with exit code: 1
set -euo pipefail
numbr_match=$(find logs/log_proj | grep "$name" | wc -l);
How could I solve this?
In this individual case, you should probably use
find logs/log_proj -name "*$name*" | wc -l
More generally, you can run grep in a subshell and trap the error.
find logs/log_proj | ( grep "$name" || true) | wc -l
... though of course grep | wc -l is separately an antipattern;
find logs/log_proj | grep -c "$name" || true
I don't know why you are using -e and pipefail when you don't want to have this behaviour. If your goal is just to treat exit code 2 (by grep) as error, but exit code 1 as no-error, you could write a wrapper script around grep, which you
call instead of grep:
#!/bin/bash
# This script behaves exactly like grep, only
# that it returns exit code 0 if there are no
# matching lines
grep "$#"
rc=$?
exit $((rc == 1 ? 0 : rc))

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

What is wrong in this Shell script?

I keep getting unary operator expected. It seems PRC is not being assigned the value.
PRC=`ps -ef | grep test | wc -l`
if [ ${PRC} -eq 1 ]
then
echo "Congrats"
Note that ps -ef | grep test will usually include the grep process in the output, which you probably don't want. A "clever trick" to avoid this is to match for the string "test" with a regular expression that is not the simple string "test":
$ ps -ef | grep test
jackman 27787 24572 0 09:53 pts/2 00:00:00 grep --color=auto test
$ ps -ef | grep test | wc -l
1
versus
$ ps -ef | grep '[t]est'
(no output)
$ ps -ef | grep '[t]est' | wc -l
0
I do this often enough that I wrote this bash function psg (for "ps grep"):
psg () {
local -a patterns=()
(( $# == 0 )) && set -- $USER # no arguments? vanity search
for arg do
patterns+=("-e" "[${arg:0:1}]${arg:1}")
done
ps -ef | grep "${patterns[#]}"
}
You could also just use
pgrep -f test
Don't forget your closing "fi":
PRC=`ps -ef | grep test| wc -l`
if [ "${PRC}" -eq 1 ]
then
echo "Congrats"
fi
You didn't mention what shell, but this works in bash.
Save a process by using the -c (count) option to grep:
PRC=`ps -ef | grep -c test`
Be advised your pipeline includes in the count the grep command itself, so as you mentioned in your comment above your count is most likely misleading as it is only counting itself. Instead, use this:
PRC=`ps -ef | grep -c [t]est`
This will match commands with "test" in them but not the grep command itself. This is because this is using a regular expression that matches a word starting with a "t". Your command starts with a square bracket so it won't match itself. Resist doing a "grep test | grep -v grep" which is sloppy and just unnecessarily uses a process.

Getting a string from a line that follows a certain character

From a file I'm retrieving the last line using the following cmd;
tail -n 1 build.log
The output looks like this:
1477101542,,ui,say,--> amazon-ebs: AMIs were created:\n\nus-east-1: ami-63237174\nus-west-1: ami-21236841\nus-west-2: ami-27872347
I'm trying to fetch the string after us-east-1:, us-west-1: & us-west-2 using the following grep commands:
echo | tail -n 1 build.log | egrep -m1 -oe 'us-east-1: ami-.{8}' | egrep -m1 -oe 'ami-.{8}'
I run this cmd three times for each condition. Is there a better way to do this?
If the order in which the regions appear is fixed, you can simply do:
$ echo | tail -n 1 build.log | egrep -o 'ami-.{8}'
ami-63237174
ami-21236841
ami-27872347
If you want to extract the region names and you have GNU grep, try:
$ echo | tail -n 1 build.log | grep -Po 'us-[^:]+(?=: ami-.{8})'
us-east-1
us-west-1
us-west-2
To get both region names and associated values:
$ echo | tail -n 1 build.log | egrep -o 'us-[^:]+: ami-.{8}'
us-east-1: ami-63237174
us-west-1: ami-21236841
us-west-2: ami-27872347

Aix command help in Echo

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`

Resources