What does this mean?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What does this mean?
# 1  
Old 02-27-2012
What does this mean?

Ok I am a PC/Network technician at a local energy company, the how or why isn't really important, but I am a linux amatuer and I need to know what these scripts mean.

Code:
1. #!/bin/bash
2. #
3. date
4. echo 'Would you like to execute the command (y/n)? \c'
5. read ANSWER
6. if [ $ANSWER = "y" -o $ANSWER = "Y" ]
7. then
8. find /media/backup -name '*.tar.gz' -type f -daystart -ctime +7 -exec rm -f {} \;
9. else
10. find /media/backup -name '*.tar.gz' -type f -daystart -ctime +7 -print
11. fi


Code:
1. #!/bin/bash
2. #
3. date
4. case "$1" in
5. start)
6. echo '-- executing command in start section --'
7. SERVSTAT=`service httpd status | grep running`
8. if [ ${#SERVSTAT} -eq 0 ]
9. then
10. service httpd start
11. else
12. echo 'service already running'
13. fi
14. ;;
15. stop)
16. echo '-- executing command in stop section --'
17. SERVSTAT=`service httpd status | grep stopped`
18. if [ ${#SERVSTAT} -eq 0 ]
19. then
20. service httpd stop
21. else
22. echo 'service already stopped'
23. fi
24. ;;
25. restart)
26. echo '-- executing command in restart section --'
27. service httpd restart
28. ;;
29. esac

If someone could explain these two scripts to me in a detailed manner, line by line detail would be nice. This will help me immensely in my job, thanks!

Last edited by methyl; 02-28-2012 at 09:27 AM.. Reason: remove microfont, add code tags
# 2  
Old 02-27-2012
First script, comments in line with the code:

Code:
#!/bin/bash
# the previous line indicates the shell that should interpret the script
#
date                    # print the current date/time to the tty
echo 'Would you like to execute the command (y/n)? \c'      # prompt user \c keeps the cursor on the same line
read ANSWER                                                 # read response from keyboard; assign to variable ANSWER
if [ $ANSWER = "y" -o $ANSWER = "Y" ]                       # test user's answer if Y or y execute the first find
then
    # this find command searches the directory /mdeia/backup, and all subdirectories, for any
    # tiles named *.targ.gz and were last modified more than 7 days ago. For each file it
    # finds, it removes the file.
    find /media/backup -name '*.tar.gz' -type f -daystart -ctime +7 -exec rm -f {} \;
else
    # this find command (executed if user does not type y) finds the same files, but
    # just prints the list on the terminal -- a previvew of what would have been deleted
    # if the user typed y to the prompt.
    find /media/backup -name '*.tar.gz' -type f -daystart -ctime +7 -print
fi



---------- Post updated at 22:40 ---------- Previous update was at 22:32 ----------

Explanation of the second script

Code:
#!/bin/bash
#
date
case "$1" in            # examine the first parameter passed to the script
                        # it is expected to be start, stop or restart

    start)              # execute the following code when $1 is start
        echo '-- executing command in start section --'
        SERVSTAT=`service httpd status | grep running`      # execute the command service, pick out lines with running and assign to a variable
        if [ ${#SERVSTAT} -eq 0 ]           # if the length of the contents of SERVSTAT are zero (nothing found by command)
        then
            service httpd start             # start the service
        else
            echo 'service already running'  # just write that the service is already running
        fi
        ;;      # end of code block, skip to esac


    stop)               # execute this code block when $1 is stop
        echo '-- executing command in stop section --'
        SERVSTAT=`service httpd status | grep stopped`      # execute the service command and search for the word stopped
        if [ ${#SERVSTAT} -eq 0 ]
        then
            service httpd stop                              # if the command did not find stopped service, then stop it
        else
            echo 'service already stopped'                  # else just echo state to the user
        fi
        ;;                                                  # end of code block, skip to esac

    restart)
        echo '-- executing command in restart section --'
        service httpd restart                               # execute the restart command
        ;;                                                  # end of code block, skip to esac
 esac



Hope this helps.

Last edited by jim mcnamara; 02-28-2012 at 12:00 PM..
# 3  
Old 02-28-2012
Just a tip, become familiar with the "man" command, which shows you the manual page for a command i.e :
Code:
$ man date

to learn about the date command and its options. Of course these days you can ask the google too. When I was learning I did what you are doing, taking an existing script and studying it line by line, doing a man and reading about each command I was not familiar with. When you learn what it does, add a comment above or to the right of it, like what agama did for you. Keep a copy of your edited script in your own directory for reference. Pretty soon it will start clicking. Then you need to learn the vi editor!

Perusing posts here will be a help too, use the search functionality for a specific question. Believe me, whatever question you have, you will not be the first to ask it. Use a meaningful subject line for your post, you'll get more views and replies, plus future searchers will have a better chance at finding information.

People here are real helpful, but mention the system and shell you are using too when asking a question, it matters. Be thankful there is a resource like this site. Back in the day when I asked a question, the response was usually a gruff "RTFM!"! lol

Last edited by gary_w; 02-28-2012 at 11:38 AM..
# 4  
Old 02-28-2012
So what does the # sign by itself mean on line 2? Just out of curiosity and by the way these descriptions have been extremly helpful.
# 5  
Old 02-28-2012
The pound sign by itself starts a comment. It is not needed here since no comment follows.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question