LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need annotated explanation of the script !! (https://www.linuxquestions.org/questions/linux-newbie-8/need-annotated-explanation-of-the-script-808767/)

ruchika_sachdeva 05-19-2010 02:01 AM

need annotated explanation of the script !!
 
Hi i am new to linux and don't know about shell scripts. I need the detailed description of what each line of this shell script does.


1 #!/bin/bash
2 # makewhatis: create the whatis database
3
4 program=`basename $0`
5
6 # In case both /usr/man and /usr/share/man exist, the former is local
7 # and should be first.
8 # It is a bug to add /var/cache/man to DEFCATPATH.
9 dm=
10 for d in /usr/man /usr/share/man /usr/X11R6/man /usr/local/man
11 do
12 if [ -d $d ]; then
13 if [ x$dm = x ]; then dm=$d; else dm=$dm:$d; fi
14 fi
15 done
16 DEFMANPATH=$dm
17 dc=
18 for d in /usr/man/preformat /usr/man /usr/share/man/preformat /usr/share/man
19 do
20 if [ -d $d ]; then
21 if [ x$dc = x ]; then dc=$d; else dc=$dc:$d; fi
22 fi
23 done
24 DEFCATPATH=$dc
25
26 # make a single big /var/cache/man/whatis file,
27 DEFWHATISDIR=/var/cache/man
28 DEFWHATIS=$DEFWHATISDIR/whatis
29
30 AWK=/bin/awk
31
32 # Find a place for our temporary files.
33
34 if TMPFILE=$(mktemp /tmp/whatis.XXXXXX)
35 then
36
37 # make sure TMPFILE is deleted if program is killed or terminates
38 # (just delete this line if your shell doesnt know about trap)
39 trap "rm -rf $TMPFILE" 0
40 trap "rm -rf $TMPFILE; exit 255" 1 2 3 15
41
42 # default find arg: no directories, no empty files
43 findarg0="-type f -size +0"
44
45 topath=manpath
46
47 defmanpath=$DEFMANPATH
48 defcatpath=
49
50 if [ -n "$MANSECT" ]; then
51 sections=$MANSECT
52 else
53 sections=`$AWK '($1 == "MANSECT") { print $2 }' /etc/man.config`
54 if [ x"$sections" = x ]; then
55 sections="1:1p:8:2:3:3p:4:5:6:7:9:0p:tcl:n:l"
56 fi
57 fi
58 sections=`echo $sections | sed -e 's/:/ /g'`
59
60 whatisdb=$DEFWHATIS
61
62 for name in "$@"
63 do
64 if [ -n "$setsections" ]; then
65 setsections=
66 sections=$name
67 continue
68 fi
69 if [ -n "$setwhatis" ]; then
70 setwhatis=
71 whatisdb=$name
72 continue
73 fi
74 case $name in
75 --version|-V)
76 echo "$program from man-1.6f"
77 exit 0;;
78 -c) topath=catpath
79 defmanpath=
80 defcatpath=$DEFCATPATH
81 continue;;
82 -s) setsections=1
83 continue;;
84 -o) setwhatis=1
85 continue;;
86 -U) [ -f "$whatisdb" ] && findarg="-cnewer $whatisdb"
87 update=1
88 continue;;
89 -u) findarg="-ctime 0"
90 update=1
91 continue;;
92 -v) verbose=1
93 continue;;
94 -w) manpath=`man --path`
95 catpath=$manpath
96 continue;;
97 -*) echo "Usage: makewhatis [-s sections] [-u] [-v] [-w] [manpath] [-c [catpath]] [-o whatisdb]"
98 echo " This will build the whatis database for the man pages"
99 echo " found in manpath and the cat pages found in catpath."
100 echo " -s: sections (default: $sections)"
101 echo " -u: update database with pages added today"
102 echo " -U: update database with pages added since last makewhatis run"
103 echo " -v: verbose"
104 echo " -o: location of whatis database (default: $DEFWHATIS)"
105 echo " -w: use manpath obtained from \`man --path\`"
106 echo " [manpath]: man directories (default: $DEFMANPATH)"
107 echo " [catpath]: cat directories (default: the first existing"
108 echo " directory in $DEFCATPATH)"
109 exit;;
110 *) if [ -d $name ]
111 then
112 eval $topath="\$$topath":$name
113 else
114 echo "No such directory $name"
115 exit
116 fi;;
117 esac
118 done
119
120 manpath=`echo ${manpath-$defmanpath} | tr : ' '`
121 if [ x"$catpath" = x ]; then
122 for d in `echo $defcatpath | tr : ' '`
123 do
124 if [ -d $d ]; then catpath=$d; break; fi
125 done
126 fi
127 catpath=`echo ${catpath} | tr : ' '`

chrism01 05-19-2010 02:06 AM

I suggest you read the following:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

grail 05-19-2010 02:12 AM

+1 to chrism01's suggestion ... maybe you could have even just googled bash

MTK358 05-19-2010 07:19 AM

And The LinuxCOmmand tutorial in my sig has a great, beginner-friendly bash scripting tutorial.

pixellany 05-19-2010 07:23 AM

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

I cannot believe that you thought ANYONE would go line-by-line for 127 lines and explain what each one did!!!

Please feel free to start a new thread which meets the homework guidelines above.


All times are GMT -5. The time now is 03:57 PM.