LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-24-2005, 03:02 PM   #1
lowpro2k3
Member
 
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 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.
 
Old 03-24-2005, 03:29 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
Old 03-24-2005, 03:39 PM   #3
lowpro2k3
Member
 
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...
 
  


Reply


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


All times are GMT -5. The time now is 12:25 PM.

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