Pokrewne
- Strona Główna
- Linux administracja sieciami zaawansowane ( 554 strony )
- Sams' Teach Yourself Linux In 24 Hours
- Teach yourself linux in 24 hours
- 100 linux tips and tricks
- linux podrecznik administratora (PL) (2)
- !!linux podrecznik administratora (PL)
- Linux podrecznik administratora sieci (2)
- Linux Network Administrators' Guide
- securing redhat
- ÂŚw. Jan Od Krzyża Dzieła
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- staffik.htw.pl
[ Pobierz całość w formacie PDF ]
.Usually used in while loop.Syntax: getopts {optsring} {variable1}getopts is used by shell to parse command line argument.optstring contains the option letters to berecognized; if a letter is followed by a colon, the option is expected to have an argument, which shouldbe separated from it by white space.Each time it is invoked, getopts places the next option in the shellvariable variable1, When an option requires an argument, getopts places that argument into the variableOPTARG.On errors getopts diagnostic messages are printed when illegal options or missing optionarguments are encountered.If an illegal option is seen, getopts places ? into variable1.For e.g.We havescript called ani which has syntax asani -n -a -s -w -dOptions: These are optional argument-n name of animalhttp://www.freeos.com/guides/lsst/maspc.htm (13 of 17) [17/08/2001 17.42.32]Linux Shell Script Tutorial-a age of animal-s sex of animal-w weight of animal-d demo values (if any of the above options are usedtheir values are not taken)$ cat > ani## Usage: ani -n -a -s -w -d### help_ani() To print help#help_ani(){echo "Usage: $0 -n -a -s -w -d"echo "Options: These are optional argument"echo " -n name of animal"echo " -a age of animal"echo " -s sex of animal "echo " -w weight of animal"echo " -d demo values (if any of the above options are used "echo " their values are not taken)"exit 1}##Start main procedure###Set default value for variable#isdef=0na=Motiage="2 Months"sex=Maleweight=3Kg##if no argument#if [ $# -lt 1 ]; thenhelp_anifiwhile getopts n:a:s:w:d optdocase "$opt" inn) na="$OPTARG";;a) age="$OPTARG";;s) sex="$OPTARG";;w) weight="$OPTARG";;d) isdef=1;;\?) help_ani;;esacdonehttp://www.freeos.com/guides/lsst/maspc.htm (14 of 17) [17/08/2001 17.42.32]Linux Shell Script Tutorialif [ $isdef -eq 0 ]thenecho "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"elsena="Pluto Dog"age=3sex=Maleweight=20kgecho "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"fiSave it and run as follows$ chmod +x ani$ ani -n Lassie -a 4 -s Female -w 20Kg$ ani -a 4 -s Female -n Lassie -w 20Kg$ ani -n Lassie -s Female -w 20Kg -a 4$ ani -w 20Kg -s Female -n Lassie -a 4$ ani -w 20Kg -s Female$ ani -n Lassie -a 4$ ani -n Lassie$ ani -a 2See because of getopts, we can pass command line argument in different style.Following are invalidoptions for ani script$ ani -nLassie -a4 -sFemal -w20KgHere no space between option and their value.$ ani -nLassie-a4-sFemal-w20Kg$ ani -n Lassie -a 4 -s Female -w 20Kg -c MammalHere -c is not one of the options.More examples of Shell Script (Exercise for You :-)First try to write this shell script, as exercise, if any problem or for sample answer to this Shell script openthe shell script file supplied with this tutorial.Q.1.How to write shell script that will add two nos, which are supplied as command line argument, and ifthis two nos are not given show error and its usageAnswer: See Q1 shell Script.Q.2.Write Script to find out biggest number from given three nos.Nos are supplies as command lineargument.Print error if sufficient arguments are not supplied.Answer: See Q2 shell Script.Q.3.Write script to print nos as 5,4,3,2,1 using while loop.Answer: See Q3 shell Script.Q.4.Write Script, using case statement to perform basic math operation asfollows+ addition- subtractionx multiplication/ divisionThe name of script must be 'q4' which works as follows$./q4 20 / 3, Also check for sufficient command line argumentsAnswer: See Q4 shell Script.Q.5.Write Script to see current date, time, username, and current directoryAnswer: See Q5 shell Script.Q.6.Write script to print given number in reverse order, for eg.If no is 123 it must print as 321.http://www.freeos.com/guides/lsst/maspc.htm (15 of 17) [17/08/2001 17.42.32]Linux Shell Script TutorialAnswer: See Q6 shell Script.Q.7.Write script to print given numbers sum of all digit, For eg.If no is 123 it's sum of all digit will be1+2+3 = 6.Answer: See Q7 shell Script.Q.8 [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl agnieszka90.opx.pl
.Usually used in while loop.Syntax: getopts {optsring} {variable1}getopts is used by shell to parse command line argument.optstring contains the option letters to berecognized; if a letter is followed by a colon, the option is expected to have an argument, which shouldbe separated from it by white space.Each time it is invoked, getopts places the next option in the shellvariable variable1, When an option requires an argument, getopts places that argument into the variableOPTARG.On errors getopts diagnostic messages are printed when illegal options or missing optionarguments are encountered.If an illegal option is seen, getopts places ? into variable1.For e.g.We havescript called ani which has syntax asani -n -a -s -w -dOptions: These are optional argument-n name of animalhttp://www.freeos.com/guides/lsst/maspc.htm (13 of 17) [17/08/2001 17.42.32]Linux Shell Script Tutorial-a age of animal-s sex of animal-w weight of animal-d demo values (if any of the above options are usedtheir values are not taken)$ cat > ani## Usage: ani -n -a -s -w -d### help_ani() To print help#help_ani(){echo "Usage: $0 -n -a -s -w -d"echo "Options: These are optional argument"echo " -n name of animal"echo " -a age of animal"echo " -s sex of animal "echo " -w weight of animal"echo " -d demo values (if any of the above options are used "echo " their values are not taken)"exit 1}##Start main procedure###Set default value for variable#isdef=0na=Motiage="2 Months"sex=Maleweight=3Kg##if no argument#if [ $# -lt 1 ]; thenhelp_anifiwhile getopts n:a:s:w:d optdocase "$opt" inn) na="$OPTARG";;a) age="$OPTARG";;s) sex="$OPTARG";;w) weight="$OPTARG";;d) isdef=1;;\?) help_ani;;esacdonehttp://www.freeos.com/guides/lsst/maspc.htm (14 of 17) [17/08/2001 17.42.32]Linux Shell Script Tutorialif [ $isdef -eq 0 ]thenecho "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"elsena="Pluto Dog"age=3sex=Maleweight=20kgecho "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"fiSave it and run as follows$ chmod +x ani$ ani -n Lassie -a 4 -s Female -w 20Kg$ ani -a 4 -s Female -n Lassie -w 20Kg$ ani -n Lassie -s Female -w 20Kg -a 4$ ani -w 20Kg -s Female -n Lassie -a 4$ ani -w 20Kg -s Female$ ani -n Lassie -a 4$ ani -n Lassie$ ani -a 2See because of getopts, we can pass command line argument in different style.Following are invalidoptions for ani script$ ani -nLassie -a4 -sFemal -w20KgHere no space between option and their value.$ ani -nLassie-a4-sFemal-w20Kg$ ani -n Lassie -a 4 -s Female -w 20Kg -c MammalHere -c is not one of the options.More examples of Shell Script (Exercise for You :-)First try to write this shell script, as exercise, if any problem or for sample answer to this Shell script openthe shell script file supplied with this tutorial.Q.1.How to write shell script that will add two nos, which are supplied as command line argument, and ifthis two nos are not given show error and its usageAnswer: See Q1 shell Script.Q.2.Write Script to find out biggest number from given three nos.Nos are supplies as command lineargument.Print error if sufficient arguments are not supplied.Answer: See Q2 shell Script.Q.3.Write script to print nos as 5,4,3,2,1 using while loop.Answer: See Q3 shell Script.Q.4.Write Script, using case statement to perform basic math operation asfollows+ addition- subtractionx multiplication/ divisionThe name of script must be 'q4' which works as follows$./q4 20 / 3, Also check for sufficient command line argumentsAnswer: See Q4 shell Script.Q.5.Write Script to see current date, time, username, and current directoryAnswer: See Q5 shell Script.Q.6.Write script to print given number in reverse order, for eg.If no is 123 it must print as 321.http://www.freeos.com/guides/lsst/maspc.htm (15 of 17) [17/08/2001 17.42.32]Linux Shell Script TutorialAnswer: See Q6 shell Script.Q.7.Write script to print given numbers sum of all digit, For eg.If no is 123 it's sum of all digit will be1+2+3 = 6.Answer: See Q7 shell Script.Q.8 [ Pobierz całość w formacie PDF ]