Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I have a script I am running: #!/bin/sh # Network status. # Pass name of network interface, such as eth0, as $1 interface=$1 #Save value, because we overright $1 ...
  1. #1
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    103

    [SOLVED] Scripting Question

    Hi,

    I have a script I am running:

    #!/bin/sh
    # Network status.
    # Pass name of network interface, such as eth0, as $1

    interface=$1 #Save value, because we overright $1

    stats=`netstat -i | grep $interface | tail -1`

    set $stats
    echo $4
    echo $8

    echo `uptime`
    echo `hostname`

    I want to write a value to a file on disk, then get it later... What command, or commands will allow me to write a value to to a file, and what command or commands will allow me to get it back? Are variables typed in this environment? Looking to save off the if byte value to a file, then get it later then sub it from teh current then assign to a varib and graph in mrtg...

    Thanks,
    Dave

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Variables in both the Bourne shell (sh) and Bash are untyped. So there's that answer.

    It is possible that you need the value to be stored in a file for other reasons, but it seems silly to store a value in a file and then retrieve it. You can always simply assign it to another variable if you need to reuse a particular one.

    In any event, writing and reading from files in Bash is fairly simple:
    Code:
    echo "hello" > file # this creates file "file" if it doesn't exist, and removes all the content of "file" if it does exist.  It then writes "hello" to the file.
    echo "hello" >> file # this creates file "file" if it doesn't exist, and appends "hello" to the end of the file
    read variable < file # this reads the first line of file "file" into the variable $variable
    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    103
    Hi,

    Thanks for teh answer, I'll mess with this later today! I need to keep the value between runs of the script. What I am doing is to save off the current byte count, then on next invocation recall it, subtract it from the current byte count, and then have the number of bytes that moved between runs... If there is a better way, I am all ears!!!

    Thanks again!!!
    Dave

  4. #4
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    103
    Thanks again, here is the finished script...
    #!/bin/sh
    # Network status.
    # Pass name of network interface, such as eth0, as $1

    #Preload some items for use
    #grab old value from file named net2mrtg.save and give it a name
    read oldbytein < net2mrtg.data0
    read oldbyteout < net2mrtg.data1

    #Give Command Tail $1 a name
    interface=$1

    #Generate a string to fetch data from
    stats=`netstat -i | grep $interface | tail -1`

    #Use that string
    set $stats

    #save current byte counts to files
    echo $4 > net2mrtg.data0
    echo $8 > net2mrtg.data1

    #Move current value into a variable for use later
    newbytein=$4
    newbyteout=$8

    #Do the math...
    usedbytein=`expr $newbytein - $oldbytein`
    usedbyteout=`expr $newbyteout - $oldbyteout`
    #Feed to MRTG
    echo $usedbytein
    echo $usedbyteout
    echo `uptime`
    echo `hostname`

    Thanks again for the help folks!!!

    Dave

Posting Permissions

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