LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash - how to write logical expressions? (https://www.linuxquestions.org/questions/programming-9/bash-how-to-write-logical-expressions-305610/)

lowpro2k3 03-24-2005 03:02 PM

Bash - how to write logical expressions?
 
I'm not that great at bash programming, and I have a class UNIX/Linux scripting. I've come across this a few times trying this assignment and the last ones, but now I want to figure it out.

I'm writing a script that provides a little wrapper around the 'ps aux' command. Basically there can be 0-3 arguments (-c, -u, -p):

-c - display all processes from the 'ps aux' output with non-zero CPU usage
-u - this requires an argument of a user to search (display processes owned by user entered)
-p - display a process by process ID entered

I'm getting all the cmd-line options from getopts - thats working fine. I'm kind of a bash newb, but I understand code logic pretty well for a first-year. I made 2 files, ps+ which controls startup flow and ps+fns.sc which has the main functions in it. In ps+ I do a while loop around my getopts like so (also note the variables I use):

Code:

declare USER_INPUT=""
declare PID_INPUT=""
declare NONZERO_FLAG=0

source "ps+fns.sc"

while getopts u:p:c arguments 2>/dev/null
do
  case $arguments in
    # I make sure that 2 instances of -u are passed to the program,
    # exiting if the user does. The function this case, and a few other
    # places calls is a simple error message and exit 1 command
    u) if [ "$USER_INPUT" = "" ]
        then
            USER_INPUT=$OPTARG    # copy argument's option into $USER_INPUT
        else
            exitError
        fi;;

    p) if [ "$PID_INPUT" = "" ]
        then
            PID_INPUT=$OPTARG
        else
            exitError
        fi;;

    c) NONZERO_FLAG=1;;
  \?) exitError;;
  esac
done


# Call the 'master controller', checks what arguments
# are set and launches appropriate reg-exp on it
psController



This is all starting to work nicely for me after being at school till 3am last night, but I have a problem. The psController function thats defined in a seperate file does some outputting before it checks the values of the arguments. Thats fine, if I can do what I would do in C++ (C++/pseudocode below) Before my function call to psController, I would like to first do a check that see's if any options were set. A simple if statement with some logical &&'s thrown in there should do it. Thats how I'd do it in C++ at least. If no options are set (ie $USER_INPUT = "", $PID_INPUT = "", NONZERO_FLAG=0) I'd like to simply perform an exit 1 inside the if statement. See code:


Code:

#include <cstdlib>

// ...
if(USER_INPUT == "" && PID_INPUT == "" && NONZERO_FLAG == 0)
    exit 1;

// call function psController - pretend i'm passing 3 vars by reference
psController(USER_INPUT, PID_INPUT, NONZERO_FLAG);

Now in C++ I'd probably use a bool instead of a 1/0 flag, but thats besides the point. I just want to understand how to write those logical expressions using AND's and OR's, because I use them quite alot in C++ and java when I program in java.

BIG thanks for any help, I'll be working on other parts of the assignment until then. I can finish it properly without worrying about the situation I mentioned, but thats just my most recent example of something I'm trying to accomplish. For our exam we have to write bash scripts on the exam paper, so I need to code and practice now on a computer while I can so I do alrite on the exam.

TheLinuxDuck 03-24-2005 03:29 PM

A useless but useful example:
Code:

#!/bin/bash

if [ -z "$1" ] && [ `pwd` = "/root" ]; then
  echo "defaulting to root dir"
elif [ -z "$1" ]; then
  echo "You don't have permission"
fi

You can also say
Code:

if [ -z "$1" -a `pwd` = "/root" ]; then
but I don't find that as readable.

lowpro2k3 03-24-2005 03:39 PM

Ahhh the one thing i didnt think of, splitting the square brackets apart. I tried the opposite (nesting brackets) and got more errors when I tried.

Thanks so much! I agree, the first example looks much more readable...


All times are GMT -5. The time now is 11:55 AM.