Find the answer to your Linux question:
Results 1 to 3 of 3
I am trying to write a Bash script that will take a list of strings and search a file for each of the strings and output that to another file. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2013
    Posts
    9

    Bash Script Getopts Arguments


    I am trying to write a Bash script that will take a list of strings and search a file for each of the strings and output that to another file.

    My biggest roadblock so far is to get the correct usage to interpret the string variables. I am taking a program that I have already written and reworking the script to some searching.

    So the usage is supposed to be:

    ./psmonitor.sh [list of parameters] [-t<seconds>] [-n<count>]

    But all I can figure out how to do is do the following :
    ./psmonitor.sh [-t<seconds>] [-n<count>] [list of parameters]

    My code is
    Code:
    #!/bin/bash
    
    CURRENT=$(date) 
    	echo " " > psmonitor.txt
    usage(){
    	echo "usage $0 -t[tseconds] -n[count]"
    } #this function will print out the correct usage when the shell is run incorrectly 
    
    COUNT=5
    TIME=100
    n=1
    
    while getopts :t:n: option #getopts to pass arguments to the script
    do 
    	case "${option}"
    	 in 
    		t) TIME="$OPTARG";;
    		n) COUNT="$OPTARG";;
    		\?) case "$OPTARG" in
    			t) echo " missing time parameter." ;;
    			n) echo " missing count parameter." ;;
    			\?) echo "Error: unknown option -$option";;  #catch all error message
    			esac
    			usage #print usage to screen
    			exit 0
    			;;
    	esac 
    done
    shift $(($OPTIND - 1))
    while [ $COUNT -gt 0 ]; do
    	echo "$CURRENT">> psmonitor.txt
    	
    	ps aux |tr -s '  ''  ' | cut -f1,2,11 -d ' '>> psmonitor.txt  #run command ps -e to list all process and append it to thelog file
    	
    	echo " "
    	trap "rm psmonitor.txt; echo Interupt encountered, exiting;  exit 1" SIGHUP SIGINT SIGTERM #trap to catch interupts , deletes output and prompts user with exit condition
    	let COUNT=COUNT-1 #count down mechanism for loop
    	sleep $TIME  #argument to wait so many seconds between iterationso fo main loop
    done
    grep $1 psmonitor.txt > file.txt
    
    echo "Program is written by dresden0526"
    So I can get ./psmonitor.sh [-t<seconds>] [-n<count>] [list of parameters]

    I cannot for the life of me figure out how to get :
    ./psmonitor.sh [list of parameters] [-t<seconds>] [-n<count>]
    to work

    I have read through all the getopts tutorials I could find. Does anyone know how I could get the code to allow me to put the list of parameters first?

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    you could loop through parameters, putting parameters into an array and shifting until an option is met (parameter starts with a dash (-)), then initiate the while getopts
    Code:
    until [[ $1 == -* ]]
    do
       array+=( "$1" )
       shift
    done
    
    while getopts
    do
       :...
    done

  3. #3
    Just Joined!
    Join Date
    Jan 2013
    Posts
    9
    Quote Originally Posted by watael View Post
    hi,

    you could loop through parameters, putting parameters into an array and shifting until an option is met (parameter starts with a dash (-)), then initiate the while getopts
    Code:
    until [[ $1 == -* ]]
    do
       array+=( "$1" )
       shift
    done
    
    while getopts
    do
       :...
    done
    This seems to work thank you

    Thank you for your input

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •