Announcement

Collapse
No announcement yet.

A couple of BASH scripting questions

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    A couple of BASH scripting questions

    These should be easy;

    #1) What is the function of -f in a script? Example: [ -f $AVAILABLE ] || exit 0

    #2) Does the case statement below pick the first answer in the list and then exit?
    Code:
    	case $governors in
    		*interactive*)
    			GOVERNOR="interactive"
    			break
    			;;
    		*ondemand*)
    			GOVERNOR="ondemand"
    			case $(uname -m) in
    				ppc64*)
    					SAMPLING=100
    				;;
    			esac
    			break
    			;;
    		*powersave*)
    			GOVERNOR="powersave"
    			break
    			;;
    		*)
    			exit 0
    			;;
    	esac
    This is a snippet from /etc/init.d/ondemand. i believe what this does it cycle through the list of available governors until it finds one then uses it. On my system, "interactive" isn't available so "ondemand" get used. "powersave" is also available but the above statement uses "ondemand" because it's ahead of "powersave". If I rearranged the order of the stanzas (putting powersave above ondemand) then it would set powersave rather than ondemand. Is this right?

    Please Read Me

    #2
    Originally posted by oshunluvr View Post
    These should be easy;

    #1) What is the function of -f in a script? Example: [ -f $AVAILABLE ] || exit 0

    #2) Does the case statement below pick the first answer in the list and then exit?
    Code:
    	case $governors in
    		*interactive*)
    			GOVERNOR="interactive"
    			break
    			;;
    		*ondemand*)
    			GOVERNOR="ondemand"
    			case $(uname -m) in
    				ppc64*)
    					SAMPLING=100
    				;;
    			esac
    			break
    			;;
    		*powersave*)
    			GOVERNOR="powersave"
    			break
    			;;
    		*)
    			exit 0
    			;;
    	esac
    This is a snippet from /etc/init.d/ondemand. i believe what this does it cycle through the list of available governors until it finds one then uses it. On my system, "interactive" isn't available so "ondemand" get used. "powersave" is also available but the above statement uses "ondemand" because it's ahead of "powersave". If I rearranged the order of the stanzas (putting powersave above ondemand) then it would set powersave rather than ondemand. Is this right?
    [ -f "$FILE" ] || exit 0

    tests for the existence of a file contained in the variable. If it does not exist, the script exits. "[" is a synonym for /usr/bin/test. If you look in /usr/bin there is an executable named "[" in that directory (older versions of UNIX used to put it in /bin). "man test" will describe all the options.

    The bourne shell's case statement checks for a match between what is contained in $governors and the regular expressions preceded by ")". It goes from top to bottom. If there is a match, it executes the block of code up to the double-semicolon (;;), then exits the case statement. *) matches anything. If none of the previous regular expressions were matched, that block is executed.

    Comment


      #3
      1: -f in a test returns true if the file exists and is a regular file. In your example, if the file name held in the AVAILABLE variable and it isn't a directory, block device etc. exists then true else false.

      2: Yes, from the posted snippet, that would appear to do what you want. It is worth backing up the file before changing it though.
      If you're sitting wondering,
      Which Batman is the best,
      There's only one true answer my friend,
      It's Adam Bloody West!

      Comment


        #4
        Great, thanks guys. I had a hard time searching for "-f" - it's too short to google


        Please Read Me

        Comment


          #5
          Originally posted by oshunluvr View Post
          Great, thanks guys. I had a hard time searching for "-f" - it's too short to google

          Maybe, but searching on how is "-f" used in a bash script found this: Bash Guide for Beginners, Chapter 7. Conditional Statements, 7.1. Introduction to if
          Windows no longer obstructs my view.
          Using Kubuntu Linux since March 23, 2007.
          "It is a capital mistake to theorize before one has data." - Sherlock Holmes

          Comment


            #6
            just remember -f will only work with files. use -d to do the same with a directory.
            Mark Your Solved Issues [SOLVED]
            (top of thread: thread tools)

            Comment


              #7
              Originally posted by oshunluvr View Post
              Great, thanks guys. I had a hard time searching for "-f" - it's too short to google
              Normally case would not "cycle". But governors contains longer string:

              conservative ondemand userspace powersave performance

              So indeed wildcard will match any of the patterns in that string, in order. Easy way apparently to give preference, although traditionally, case wouldn't be used for that. In procedural or object-oriented language, you might need to do list lookup for that, and need more structures.

              Comment


                #8
                Originally posted by quaksoul1 View Post
                . Easy way apparently to give preference, although traditionally, case wouldn't be used for that.
                In "traditional" shell scripts case statements would be used for their relatively predictable pattern matching; doing it in an if statement was limited and treacherous, partly owing to quoting issues. I think the inner case statement is an example.
                I'll spare you my rant about scripts clinging to 30 year old ways.

                Regards, John Little
                Regards, John Little

                Comment

                Working...
                X