Find the answer to your Linux question:
Results 1 to 7 of 7
this works finally -- -- almost -- the second part of this script the while loop I got off the Internet and changed it from using feh to using hsetroot ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2013
    Posts
    20

    command line arguments and scripts


    this works finally -- -- almost --

    the second part of this script the while loop I got off the Internet and changed it from using feh to using hsetroot --

    then thought I could make it so that I can change the sleep time from within the loop at the the command line

    -- my first project in bash programming --

    BUT

    the main question I think is: how do I get it to take in a new value from the command line so I can change the
    sleep value within the loop do I have to use the function I wrote or was that a waste of time?

    and does or will that kill the child (if I got that subject down right I hope) or will it spawn a new child and have two little kids running around my system if i run it again from the command line? memory usage issues

    example:

    first time just from the command like ./wpch

    that is already set to 120 secs through the first if statment

    second time

    ./wpch 4 # the four meaning change it to four minutes as I used this

    g=$((wtime * 60))

    to change it from secs to minutes -- I took it up to 120 secs and sleep didn't turn into a complainer timed it even ---

    this is the second time I put it in the command line and it didn't change the value

    userx-bw-crunchbang:~/scripts$ ./wpch 3
    byte me
    2 $ wtime hello
    past first if
    2 $ wtime inside second if
    $ g inside second if
    120 that is $ g after second if
    2 $ wtime after second if
    120 I AM $ g inside while loop code here


    the 120 is from the first time I declare wtime before the function and it goes into the second if statement as I was working on my if statements then

    the other question I have is what var should I be calling in the $wtime or the $g set from wtime as this will cause me to have to change my if statements
    not a big deal to change them just something that will be needed to be done

    within the code I added some information too above the second if statement

    enter code here


    #!/bin/bash


    echo "byte me"
    wtime=2 #var int Global


    wtime()
    {




    echo $wtime " $ wtime hello"



    if [ "$wtime" = 1 ]; then

    echo $g " $ g inside first if not set yet"
    echo $wtime "$w time inside first if"

    wtime=60
    g=$wtime

    echo $g "$ g just after declar "
    echo $wtime "$ wtime just after re-declar / chang val"

    echo $g "$ g after first if inside fi"
    echo $wtime "$ wtime after first if inside fi"

    return $g

    echo $g "$ g past return"
    echo $wtime "$ wtime past return"

    exit

    echo $g "$ g past exit still before fi"
    echo $wtime "$ wtime past exit still before fi"

    fi

    echo "past first if"

    #it should pass this if wtime is == 1
    #if wtime is greater the 1 it should enter it
    #as the first if statement changed wtime to 60
    #this is where I noticed I need to do something different
    #to change the set time within the script because it did not
    #enter this if statement nor did putting a 4 after the ./wpch 4
    #instead it just starts after the first if statement and passes
    #this if statement up as the $wtime is now grater then 1 it should have
    #gone inside this next -- c c++ if I remember right

    if [ "$wtime" > 1 ]
    then

    echo $wtime "$ wtime inside second if"
    echo $g "$ g inside second if "

    g=$((wtime * 60))

    echo $g "that is $ g after second if "
    echo $wtime "$ wtime after second if "

    return $gS

    exit

    fi





    exit


    }

    wtime




    {
    while(true);
    do
    echo $g " I AM $ g inside while loop"

    echo $wtime "$ wtime -- inside loop"

    find /home/userx/Dropbox/wallpaper/ -type f \( -name '*.jpg' -o -name '*.png' \) -print0 | shuf -n 1 -z | xargs -0 hsetroot -solid "#FF0CC0" -flipd -gradient 35 -center

    echo $g "after find ---- "
    echo $wtime "$ wtime after find"


    echo $wtiime " $ wtime -- before sleep call --"
    echo $g " $ g before sleep call "



    sleep $g #sets the change time of the wall paper




    echo $g "$ g after sleep call "
    echo $wtime " $ wtime after sleep call <<<"

    done &
    }

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    673
    First off, when you defined the "function wtime" it hides the variable (same name). Also '$' inside of double quoted string that are supposed to be the '$' need to be back quoted (ie \$). Why do you have statement after the return and exit control can never get to them?

    Change your name of the function so that it does not match variable.

  3. #3
    Just Joined!
    Join Date
    Nov 2013
    Posts
    20
    Quote Originally Posted by alf55 View Post
    First off, when you defined the "function wtime" it hides the variable (same name). Also '$' inside of double quoted string that are supposed to be the '$' need to be back quoted (ie \$). Why do you have statement after the return and exit control can never get to them?

    Change your name of the function so that it does not match variable.
    statments as in echos after and every where is so I can learn where what goes its good programming ---
    that is a part of self learning where does this var go and which one goes where as everything is global in bash???

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    673
    No, not every thing in bash is global. But the way that you are writing code, everything is more less global. In no programming language will code be executed in the block after a return or exit. Bash has functions and within them you can use "local varname1 varname2 ... varnameN" to put those variables into local space. Also I do not know of any language that can have a variable and a function that has the same name in the same scope. Good programming practices are pretty much the same in most languages.

    The way that the code is written with all the excess code that can never be executed makes the code very hard to read.

    This is easier to read.

    Code:
    #!/bin/bash
    
    echo "byte me"
    wtime=2 #var int Global
    
    function calc_wtime()
    {
        echo "$wtime  \$ wtime hello"
    
        if [ "$wtime" -eq 1 ] ; then    # number compare for equal
    
            echo "$g \$g inside first if not set yet"
            echo "$wtime \$wtime inside first if"
    
            wtime=60
            g=$wtime
            echo "$g \$g just after declar "
            echo "$wtime \$wtime just after re-declar / chang val"
            echo "$g \$g after first if inside fi"
            echo "$wtime \$wtime after first if inside fi"
    
            return "$g"
        fi
    
        echo "past first if"
    
        # it should pass this if wtime is == 1
        # if wtime is greater the 1 it should enter it
        # as the first if statement changed wtime to 60
        # this is where I noticed I need to do something different
        # to change the set time within the script because it did not
        # enter this if statement nor did putting a 4 after the ./wpch 4
        # instead it just starts after the first if statement and passes
        # this if statement up as the $wtime is now grater then 1 it should have
        # gone inside this next -- c c++ if I remember right
    
        if [ "$wtime" -gt 1 ] ; then   # number compare for less then
    
            echo $wtime "$ wtime inside second if"
            echo $g "$ g inside second if "
    
            g=$((wtime * 60))
    
            echo $g "that is $ g after second if "
            echo $wtime "$ wtime after second if "
    
            return "${g}S"
        fi
    }
    
    
    calc_wtime
    
    
    {
        while true; do    # this works because there is a command called "true" that always returns 0
            echo "$g  I AM \$g inside while loop"
    
            echo "$wtime \$wtime -- inside loop"
    
            find /home/userx/Dropbox/wallpaper/ -type f \( -name '*.jpg' -o -name '*.png' \) -print0 | shuf -n 1 -z | xargs -0 hsetroot -solid "#FF0CC0" -flipd -gradient 35 -center
    
            echo "$g after find ---- "
            echo "$wtime \$wtime after find"
    
    
            echo "$wtiime \$wtime -- before sleep call --"
            echo "$g  \$g before sleep call "
    
            sleep $g    #sets the change time of the wall paper
    
            echo "$g \$g after sleep call "
            echo "$wtime \$wtime after sleep call <<<"
    
        done &
    }

  6. #5
    Just Joined!
    Join Date
    Nov 2013
    Posts
    20
    thanks for that lession -- but where did ou find the docs on how you write you if statments using -gt -eq I have yet to see anything that I have looked at telling me how to do if statments like that. from what I've learned its the = gives an asingment of a value and == is a comparison of a vale along with boolean , = < != > <= >= <> are all compairsioons and && and || are - and - or condistions

    Quote Originally Posted by alf55 View Post


    if [ "$wtime" -eq 1 ] ; then # number compare for equal

    if [ "$wtime" -gt 1 ] ; then # number compare for less then


    return "${g}S"
    [code]

  7. #6
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,649
    This is a great tutorial on the shell and bash scripting. This is another one - but it's more general and almost scarily detailed.
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  8. #7
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    673
    I started programming in 1971 and Unix shell programming in 1976 and we used books (paper back and hard back and real paper sheets with ink). At this moment I have three bash books loaned out. Getting one back on Monday. I really like books, you can write in them, correct them, add extra index entries, add stuff like "should not do this see page XXX", etc.

    The "bash" shell also has build-in help via the "help" command. Entering "help if" results in:
    Code:
    if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
        Execute commands based on conditional.
        
        The `if COMMANDS' list is executed.  If its exit status is zero, then the
        `then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
        executed in turn, and if its exit status is zero, the corresponding
        `then COMMANDS' list is executed and the if command completes.  Otherwise,
        the `else COMMANDS' list is executed, if present.  The exit status of the
        entire construct is the exit status of the last command executed, or zero
        if no condition tested true.
        
        Exit Status:
        Returns the status of the last command executed.
    Which for learning does not provide enough information.


    Startup scripts are mostly written in Borne Shell (a sub-set of bash).

    For bash the reference manual is at: Bash Reference Manual
    Beginning Bash at: http://tldp.org/LDP/Bash-Beginners-G...ners-Guide.pdf


    Also may be of interest:
    http://www.linuxtraining.co.uk/downl...se_modules.pdf
    Bourne Shell Tutorial
    Last edited by alf55; 11-28-2013 at 03:29 PM. Reason: Added info on 'help" command

Posting Permissions

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