Ksh: Test UNIX command without $? everytime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh: Test UNIX command without $? everytime
# 1  
Old 01-14-2014
Ksh: Test UNIX command without $? everytime

Hello all,

working on Solaris 10 in ksh.

Basicly, in my function, i'm trying to test that all my unix cmd's are true (exit status 0) else you flag the rcControlRule to 1 without going into spagetti mode code testing every $? in a if statement.

The mdb is probably a little tricky cause it will give something like this:

Code:
# echo ―noexec_user_stack/D‖ | mdb -k
noexec_user_stack:
noexec_user_stack: 1

And i'm looking for something like this:

Code:
# echo ―noexec_user_stack/D‖ | mdb -k
noexec_user_stack:
noexec_user_stack: 0

Bottom line i just want to know how you guys evaluate unix command without testing $? everytime... there must be a simpler way. A best practice somewhere that i miss.

Bellow code doesn't work but basicly explain what i'm trying to do.
Code:
CONFIG_FILE="/etc/system"
STACK=$(grep -q "^set noexec_user_stack=1" ${CONFIG_FILE})
STACK_LOG=$(grep -q "^set noexec_user_stack_log=1" ${CONFIG_FILE})
MDB=$(echo "noexec_user_stack/D" | mdb -k)

check_config () {
        if [[ ${STACK} -a ${STACK_LOG} -a ${MDB} ]]; then
                rcControlRule=0
        else
                rcControlRule=1
        fi
        return ${rcControlRule}
}

Maybe i should use something like:

Code:
set -A services "grep -q \"^set noexec_user_stack=1\" ${CONFIG_FILE}" "grep -q \"^set noexec_user_stack_log=1\" ${CONFIG_FILE}" "echo \"noexec_user_stack/D\" | mdb -k"

check_service () {
        i=0
        while [[ $i -lt "${#services[@]}" ]] && [[ rcControlRule -ne 1 ]]; do
                if  ! "${services[$i]}"  > /dev/null 2>&1; then
                        rcControlRule=1
                else
                        rcControlRule=0
                fi
        (( i+=1 ))
        done
        return ${rcControlRule}
}

Thanks.

Eric
# 2  
Old 01-14-2014
There are many ways to Rome. For example you could try something like this:
Code:
CONFIG_FILE="/etc/system"
if
  grep -q "^set noexec_user_stack=1" ${CONFIG_FILE}     &&
  grep -q "^set noexec_user_stack_log=1" ${CONFIG_FILE} &&
  echo "noexec_user_stack/D" | mdb -k >/dev/null 2>&1 
then
  echo all commands are OK
fi

or this:
Code:
checkconfig() {
  grep -q "^set noexec_user_stack=1" ${CONFIG_FILE}     &&
  grep -q "^set noexec_user_stack_log=1" ${CONFIG_FILE} &&
  echo "noexec_user_stack/D" | mdb -k >/dev/null 2>&1 
}

if checkconfig; then
  echo OK
fi

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-14-2014
grep -q turns a match into an exit code, and does not return the matching string.
The exit code is best directly tested in the if statement, like in Scrutinizers post.
The exit code of mdb is a bit strange, so maybe its output should be taken by another grep -q:
Code:
if
 ...
 echo "noexec_user_stack/D" | mdb -k 2>/dev/null | grep -q .
then
 ...

Or even should require the value 1:
Code:
 echo "noexec_user_stack/D" | mdb -k 2>/dev/null | grep -qw 1

BTW only /usr/xpg4/bin/grep has -q (not /usr/bin/grep).

Last edited by MadeInGermany; 01-14-2014 at 02:38 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 01-15-2014
Many thanks to you guys!

Scrutinizer: love the way you've turned the TRUE into a function. Very clean.

MIG: Never used the -w for grep. Works perfectly (i knew about the grep since i work on Solaris all the time)

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with UNIX test and wc Command

I want to xheck if a file exists that uses wildcards as only the partial filename is known using the test Command, and when it exists then output just the number of lines in the file... do not include the filename. Then this output, is it captured by the CommandOutput or the ReturnValue as I want... (2 Replies)
Discussion started by: dsinco
2 Replies

2. Shell Programming and Scripting

Test command in UNIX

Hi Team, -rwxr-xr-x 1 kmani00 system 9 Nov 08 03:29 tempfile.txt -rwxrwxrwx 1 kmani00 devgrp 0 Nov 08 03:32 testfile.txt by exec the following command, i did not get any output. > test -s tempfile.txt > a=`test -s tempfile.txt` > echo $a > by exec the... (4 Replies)
Discussion started by: kmanivan82
4 Replies

3. Shell Programming and Scripting

ksh script to test max number of parallel ssh connections

hello , I need to check how many parallel ssh connections my server can take the load of. I need to login to different host which i am planning to read from a file and then a write a loop which will do parallel ssh. Please provide suggestion on how i can write script for the same.\ Thank... (1 Reply)
Discussion started by: ABHIKORIA
1 Replies

4. Shell Programming and Scripting

recent test -e ksh incompatibility in hpux?

On a very new (11.31) hpux machine, I can no longer execute shell fragements like: if ; then . .profile.foo fi and get "ksh: test: argument expected" if I convert this to -d or -f as appropriate (which I've not had to do on older versions of hpux (11.23) nor any other unix platform... (9 Replies)
Discussion started by: Peeter Joot
9 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. UNIX for Dummies Questions & Answers

Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed: # Check to see if the 8 characters are all numbers # If not show error essage # And prompt user... (4 Replies)
Discussion started by: netmaster
4 Replies

7. Shell Programming and Scripting

KSH: Test telnet and exit

Hi, I need to do a test Telnet in KSH and if the connection is good then disconnect the telnet session with out logging in and without exiting the shell script. Example output of a good connection: $telnet xxx.xx.xx.xxx xxxx Trying xxx.xx.xx.xxx... Connected to xxx.xx.xx.xxx. Escape... (1 Reply)
Discussion started by: calex
1 Replies

8. UNIX for Dummies Questions & Answers

Ksh How to test if variable is numeric??

I'm trying to test and see whether a variable that is being extracted from a line in a file is numeric or not. I have tried everything that I can think of and I cannot figure out how to get it to work. I am trying to test and see if the string extracted contains 5 numeric digits. This is what I... (8 Replies)
Discussion started by: developncode
8 Replies

9. Shell Programming and Scripting

ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well :) I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk). Basically I want to give the user an option to enter some text which will go down as a field within a flat... (3 Replies)
Discussion started by: tugger
3 Replies

10. Shell Programming and Scripting

test if a filename exists with specified string (ksh)

I'm trying to do a simple if statement that tests if a filename exists with a user specified string. So say I have these files: Assigned_1day_after_due_chuong Assigned_1day_after_due_gallen Assigned_1day_after_due_heidenre and i'm running a script and want to know if a... (6 Replies)
Discussion started by: bob122480
6 Replies
Login or Register to Ask a Question