Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-24-2005, 03:02 PM   #1
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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 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):

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

source ""

while getopts u:p:c arguments 2>/dev/null
  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" = "" ]
             USER_INPUT=$OPTARG    # copy argument's option into $USER_INPUT

    p) if [ "$PID_INPUT" = "" ]

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

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

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:

#include <cstdlib>

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

// call function psController - pretend i'm passing 3 vars by reference
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.
Old 03-24-2005, 03:29 PM   #2
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
A useless but useful example:

if [ -z "$1" ] && [ `pwd` = "/root" ]; then
  echo "defaulting to root dir"
elif [ -z "$1" ]; then
  echo "You don't have permission"
You can also say
if [ -z "$1" -a `pwd` = "/root" ]; then
but I don't find that as readable.
Old 03-24-2005, 03:39 PM   #3
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Original Poster
Rep: Reputation: 30
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...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to write a Bash Shell any advice? CyberEd55 Programming 3 05-12-2005 01:52 PM
? on logical operators in BASH eroica Linux - Software 10 02-01-2005 01:12 PM
Bash: how to write a wrapper script? J_Szucs Programming 0 01-29-2005 05:50 PM
Bash: read/write the interface? J_Szucs Programming 7 06-04-2004 03:09 PM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 08:56 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration