How to egrep variable-Unix shell script - shell

I’m trying to validate input by using egrep and regex.Here is the line from script (c-shell):
echo $1 | egrep '^[0-9]+$'
if ($status == 0) then
set numvar = $1
else
echo "Invalid input"
exit 1
endif
If I pipe echo to egrep it works, but it also prints the variable on the screen, and this is something I don't need.

To simply suppress output you can redirect it to the null device.
echo $1 | egrep '^[0-9]+$' >/dev/null
if ($status == 0) then
set numvar = $1
else
echo "Invalid input"
exit 1
endif
You might also want to consider using the -c option to get the count of matches instead of using using the status.
Also, unless you are using csh, the status is stored in $? not in $status

grep has a -q option that suppresses output
So:
egrep -q '^[0-9]+$'

you can use awk
$ echo "1234" | awk '{print $1+0==$1?"ok":"not ok"}'
ok
$ echo "123d4" | awk '{print $1+0==$1?"ok":"not ok"}'
not ok

Related

System Variable set in bash not sticking after i go to an IF statement

apacherelease=$(curl -s "https://httpd.apache.org" | grep Released | awk '{print $4}' | perl -p -e 's/2.4.54/2.4.54-1/g') &&
apacheinstallversion=$(dnf list installed | grep httpd.x86_64|awk '{print $2}') &&
echo $apacherelease
echo $apacheinstallversion
if test "$apacheinstallversion" = "$apacherelease"; then
: variables are the same
else
: variables are different
fi
`
If I run the commands to set variable directly from the command line instead of a script the variables stick however in the script they disappear the moment I move to the if statement.
Any input would extremely help!
Corrected version:
apacherelease=$(curl -s "https://httpd.apache.org" | grep Released | awk '{print $4}' | perl -p -e 's/2.4.54/2.4.54-1/g') &&
apacheinstallversion=$(dnf list installed | grep httpd.x86_64 | awk '{print $2}')
echo "$apacherelease"
echo "$apacheinstallversion"
if [[ $apacheinstallversion == $apacherelease ]]; then
echo "variables are the same"
else
echo "variables are different" >&2
fi
use the full featured bash test [[
use == instead of =

Output not displaying after echo

I'm trying to format my output based on the result of a certain command. However, it's not printing out. Can you guys let me know what I am missing out.
VID=$(grep -iE $vvwwn ${TPAR_TEMP}/vvid_${TPAR_NAME} > ${TPAR_TEMP}/tvvid 2> /dev/null)
TVVID=$(cat ${TPAR_TEMP}/tvvid |awk '{print$3}' 2> /dev/null)
if [ "${TVVID}" = "32" ]; then
sh templ1
echo "$VID"
else
sh templ2
echo "$VID"
fi
templ1 is
#!/bin/bash
echo "========================================================================================================"
awk '
BEGIN {printf "%-27s %-6s %-32s %-8s %-8s %-9s %-4s\n" , "Name", "State", "VV_WWN", "VSize_MB", "Usr_Used_MB", "UsrCPG", "Prov"}'
echo "========================================================================================================"
The problem is with $VID been empty due to the redirect in the command grep -iE $vvwwn ${TPAR_TEMP}/vvid_${TPAR_NAME} > ${TPAR_TEMP}/tvvid, To make the problem easier fill in the variables and run the command in your terminal.

Check if file contains string1 AND NOT string2

I've got a program (prog) that takes a lot of time to produce its output.
I need to check if the output contains string1 and not string2.
Right now I do the following. It invokes prog 2 times.
if prog | grep -q 'some string' &&
! prog | grep -q 'another string'; then
echo 'OK'
else
echo 'Not OK'
fi
Is there are way to do this with only 1 invocation of prog?
Any solution involving awk or sed will do as well.
UPDATE
What I was hoping for was a one liner—that my mind cannot see—to do the trick, having GNU coreutils at my disposal.
I don't think it is possible with grep without executing prog twice, or better, saving it's output into a temporary file.
I would recommend to use awk:
awk '/string1/{a=1}/string2/{b=1}END{exit !a && b}'
You can use it in the shell script like:
if prog | awk '/string1/{a=1}/string2/{b=1}END{exit !a && b}' ; then
echo "ok"
else
echo "not ok"
fi
Note: awk treats 0 as false and 1 as true. The shell treats 0 as true and 1 as false. To reflect that we return !a && b instead of the a && !b, which might look more reasonable in the first place.
With sed, to output line that meet the requirements :
prog | sed -n ':a;$!N;s/\n//;ta;/string1/{/string2/!p}'
Update :
To test the sed command (GNU sed) :
prog | sed -n ':a;$!N;s/\n//;ta;/string1/{/string2/!{q};/string2/{q 1}}' && echo "ok" || echo "nok"
For the problem by using grep command :
prog | grep -E 'string1|string2' | tr -d "\n" | grep -v string2 | grep string1 && echo "OK" || echo "NOT OK"

If statement inside command line

I'm trying to create a shell script and the fact is, I want to change the output if the variable $output is filled. I was thinking about checking the variable with an if inside the command but I don't know if it's the correct syntax. Here is an exemple (of course that doesn't work):
ls -lisa | awk '$5 == own' own="$owner" | sort -k$column -n if [
$output ]; then print > out.txt fi
I don't know if it's going to work that way and if it's possible.
The exec built-in can change the default standard output for the rest of the running shell script. So, in this case, you would do:
if [ -n "$output" ]; then
exec >out.txt
fi
ls -lisa | awk '$5 == own' own="$owner" | sort -k$column
I'm not entirely sure what you're trying to do with the awk part, so this is just verbatim from your question.
Another option is to put the part of your script that you want to redirect into a function, and then call the function in one of two ways, redirecting the output. Example:
do_work() {
ls -lisa | awk '$5 == own' own="$owner" | sort -k$column
}
if [ -n "$output" ]; then
do_work >out.txt
else
do_work
fi
You can use the shell's "use default value" option (${variable:-default}, with /dev/stdout as the default) to do this:
ls -lisa | awk '$5 == own' own="$owner" | sort -k$column -n > "${output:-/dev/stdout}"

If or while loop inside case command positional parameters

Being relatively new to anything other than bash scripting, I have created a script to
check if a process is running
output PID's to the shell
if not prompt user input and start etc/etc.
I've moved onto positional parameters and can't see where I'm going wrong:
if [ "$1" == "" ]; then
proc_finder
elif [ $1 != "" ];then
case $1 in
-p | --process )
shift
z=$(ps aux |grep $1 |grep -v grep > /dev/null)
if [ ! -z "$z" ]; then
echo "YES"
else
echo "NO"
fi
;;
* )
echo "Usage -p (process)"
esac
fi
This always seems to return yes even when putting in -p test for example. I know im doing something fundamentally wrong, looking at the verbose output the grep -v grep is being done last hence I believe it always returnes an exit state of 0.
Shouldn't that be if [ $? -eq 0 ]?
EDIT 1
You can try this:
z=`ps aux | grep $1 | grep -v grep > /dev/null`
if [ ! -z "$z" ]; then
echo "YES"
else
echo "NO"
fi
If $z is not empty (-z: test for zero-length string) this implies the process was found with the ps command.
EDIT 2
The ps ... grep ... grep is being redirect to /dev/null. That means z will contain nothing. remove the redirection and z should have some output.
z=`ps aux | grep $1 | grep -v grep`
EDIT 3
Alternatively, you can just do this:
ps aux | grep $1 | grep -v grep > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "YES"
else
echo "NO"
fi
In this case, you are not saving the grep output. That's good if you don't really need it.

Resources