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="" 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> 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. |
A useless but useful example:
Code:
#!/bin/bash Code:
if [ -z "$1" -a `pwd` = "/root" ]; then |
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. |