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):
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" = "" ]
# 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:
if(USER_INPUT == "" && PID_INPUT == "" && NONZERO_FLAG == 0)
// 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.