Find the answer to your Linux question:
Results 1 to 7 of 7
Hi there. I got 2 scripts and I need to someone to explain their functions if possible. 1 #!/bin/sh if [ `df | sed '1d' | grep hda1 | sed ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2005
    Posts
    40

    Need help with scripts


    Hi there.
    I got 2 scripts and I need to someone to explain their functions if possible.
    1

    #!/bin/sh
    if [ `df | sed '1d' | grep hda1 | sed 's/%//' | awk '{print $5}'` -gt 75 ]
    then
    echo "WARNING. Exceeding 75%" | mailx $LOGNAME
    fi
    I know that this one warns if the HD usage is greater than 75% but I dont know what the (sed 's/%//' ) exactly does.

    2.
    #!/bin/sh
    if [ $# -lt 3 ] ; then
    echo "usage: logged-on Day Month Date (e.g.: logged-on Fri Jan 12)"
    exit
    fi
    last | head -500 | awk '{print $6,$5,$4,$1,$3}' > days_data
    cat days_data | grep $1 | grep $2 | grep $3 > log_names
    echo "Accounts logging in for $1 $2 $3"
    cat log_names | awk '{print $4,$5}' | sort +1 | sort -u
    echo -n "Total accounts: "
    cat log_names | sort -u | wc -l | tr -s " "

    On this one I have no idea.Don't ask me questions because I have no clues.
    If you getting anything out of it I will be if you can explain it to me

    Thank you very much.

  2. #2
    Just Joined! scottro's Avatar
    Join Date
    Feb 2006
    Posts
    57
    Quote Originally Posted by trialex
    Hi there.
    I got 2 scripts and I need to someone to explain their functions if possible.
    1

    #!/bin/sh
    if [ `df | sed '1d' | grep hda1 | sed 's/%//' | awk '{print $5}'` -gt 75 ]
    then
    echo "WARNING. Exceeding 75%" | mailx $LOGNAME
    fi
    I know that this one warns if the HD usage is greater than 75% but I dont know what the (sed 's/%//' ) exactly does.
    With sed the s means substitute, and in this case, it will substitute the percent sign (%) with nothing. In other words, if the result was 75% it would change it to 75.

    One way to figure it out would be to run the commands with and without the pipes and see what everything is doing--eg, run df, see the output. Then do df |sed '1d' and see what happens. Then, run df |sed '1d' | grep hda, etc. This is one way to figure out what a script does.

    Quote Originally Posted by trialex

    2.
    #!/bin/sh
    if [ $# -lt 3 ] ; then
    echo "usage: logged-on Day Month Date (e.g.: logged-on Fri Jan 12)"
    exit
    fi
    last | head -500 | awk '{print $6,$5,$4,$1,$3}' > days_data
    cat days_data | grep $1 | grep $2 | grep $3 > log_names
    echo "Accounts logging in for $1 $2 $3"
    cat log_names | awk '{print $4,$5}' | sort +1 | sort -u
    echo -n "Total accounts: "
    cat log_names | sort -u | wc -l | tr -s " "

    On this one I have no idea.Don't ask me questions because I have no clues.
    If you getting anything out of it I will be if you can explain it to me

    Thank you very much.

    Ok #!/bin/sh means it will use /bin/sh.

    if [ $# -lt 3 ] ; then

    Hopefully, you understand the pattern of if else statements. $# refers to the number of arguments. So, in this case, it's saying, if the script is called with less than 3 arguments, echo the usage. You can try this yourself in a script, something like

    Code:
    #!/bin/sh
    echo $1 $2 $3
    echo $#
    If you then (say the script is called echo.sh) try
    echo.sh hello
    It will print hello and then the number 1, for one argument.

    So, that first part says, give three arguments to this script and if not, echo the usage and exit.


    For the next part, you can try what I suggested for the sed question, see what happens when you type last. It will give the last logins. You can look at man last to get an idea.

    So, then try last | head -500. It will print the first 500 lines of the "last" command's output. (See man head if you're not familiar with it).

    Then awk, which is a handy command. In this case, awk is going to print out the fields specified. (heh, this is turning into a tutorial.) The "last command" gives several fields of information, the awk command here is taking several of those fields. A field would be something like john ttyv0 Wed March 17 blah blah. So field $1 is john, field $2 is ttyv0.
    That information which was filtered by awk is being put into a variable called days_data.

    Next, we cat days_data, the results of all we've done so far. We are trying to find in that the three arguments that the script asked for in the first place, the grep $1 grep $2 is saying, grep the first argument, the second argument, etc.

    I have to get back to work now, but see if you can now figure out the rest of what it does. It's taking arguments (the things it tells you to enter in the usage statement) and running some filtering on the results of the "last" command, then giving you back information.

    If you're not sure what is happening with any specific command, e.g. sort, take a look at sort's man page and see if you can figure it out.

    If you're still stuck post again.

  3. #3
    Just Joined!
    Join Date
    Dec 2005
    Posts
    40
    excellent help thank you

  4. #4
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    sed '1d'
    to display all lines except the first one ..
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ http://www.linuxcommand.org/ ~>

  5. #5
    Just Joined! scottro's Avatar
    Join Date
    Feb 2006
    Posts
    57
    Glad to help. As muha pointed out the 1d deletes the first line, but you didn't ask that one.

    Muha if you see this thread again, is linuxcommand.org your site, and is that your shellscripting tutorial? It's an excellent tutorial and one that I frequently recommend to people .

  6. #6
    Just Joined!
    Join Date
    Dec 2005
    Posts
    40
    Yea I missed that one but I was gettin ready to ask thanks alot.

  7. #7
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    no, linuxcommand.org is not mine
    My sig is just the quote from the site. I love their style
    And i've been there also, you configure your desktop and get bored as a newbie an think: 'now what?'
    lol
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ http://www.linuxcommand.org/ ~>

Posting Permissions

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