lolamontes69

Bash / Example Scripts

May 19th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 27.47 KB | None | 0 0
  1. Examples of small bash Scripts
  2. --------------------------------------------------------------------------------
  3. Here is a set of small scripts, which demonstrate some features of bash programming.
  4. --------------------------------------------------------------------------------
  5.  
  6. //===================================================================================
  7.  
  8. // set new prompt
  9.  
  10. //===================================================================================
  11.  
  12. PS1=">"
  13.  
  14. PS1="[${LOGNAME}@$(hostname)] # "
  15.  
  16. PS1="[${LOGNAME}] # "
  17.  
  18. PS1='$PWD $ '
  19.  
  20.  
  21. --------------------------------------------------------------------------------
  22.  
  23. //===================================================================================
  24.  
  25. // set (and automatically create) shell variable
  26.  
  27. //===================================================================================
  28.  
  29. $ test="test"
  30.  
  31. $ homedir='pwd'
  32.  
  33. string="The man said \" hello \"."
  34.  
  35. --------------------------------------------------------------------------------
  36.  
  37. //===================================================================================
  38.  
  39. // To use the variable within the shell, it is preceded by a $
  40.  
  41. //===================================================================================
  42.  
  43. homedir=$HOME
  44.  
  45. cd $homedir
  46.  
  47. --------------------------------------------------------------------------------
  48.  
  49. //===================================================================================
  50.  
  51. // how to print shell variable
  52.  
  53. //===================================================================================
  54.  
  55. echo PS1
  56.  
  57. echo $PS1
  58.  
  59. echo $USERNAME
  60.  
  61. --------------------------------------------------------------------------------
  62.  
  63. //===================================================================================
  64.  
  65. // predefined shell variables
  66.  
  67. //===================================================================================
  68.  
  69. HOME    name of users login directory
  70.  
  71. IFS     internal field separators
  72.  
  73. PATH    search path used for finding commands
  74.  
  75. PS1     shell prompt
  76.  
  77. OSTYPE
  78.  
  79. USERNAME
  80.  
  81. SHELL
  82.  
  83. --------------------------------------------------------------------------------
  84.  
  85. //===================================================================================
  86.  
  87. // The shell supports pattern matching
  88.  
  89. //===================================================================================
  90.  
  91. * Match all characters in a string
  92.  
  93. ? Match a single character
  94.  
  95.  
  96. ls *.dat
  97.  
  98. --------------------------------------------------------------------------------
  99.  
  100. //===================================================================================
  101.  
  102. // Command Substitution
  103.  
  104. //===================================================================================
  105.  
  106. today=`date`
  107.  
  108.  
  109.  
  110. --------------------------------------------------------------------------------
  111.  
  112. //===================================================================================
  113.  
  114. #---------------------------------------------------
  115.  
  116. # empty sh script program
  117.  
  118. #---------------------------------------------------
  119.  
  120. #!/bin/sh
  121.  
  122.  
  123.  
  124. --------------------------------------------------------------------------------
  125.  
  126. #---------------------------------------------------
  127.  
  128. # comments
  129.  
  130. #---------------------------------------------------
  131.  
  132. #!/bin/sh
  133.  
  134. # this is comment
  135.  
  136.  
  137.  
  138. --------------------------------------------------------------------------------
  139.  
  140. #---------------------------------------------------
  141.  
  142. # printing of string constant
  143.  
  144. #---------------------------------------------------
  145.  
  146. #!/bin/sh
  147.  
  148. echo 'hello'
  149.  
  150. echo "hello"
  151.  
  152. echo hello
  153.  
  154.  
  155.  
  156. --------------------------------------------------------------------------------
  157.  
  158. #---------------------------------------------------
  159.  
  160. # declaration and printing of string variable
  161.  
  162. #---------------------------------------------------
  163.  
  164. #!/bin/sh
  165.  
  166.  
  167.  
  168. x='Wonderful new World'
  169.  
  170. echo $x
  171.  
  172. echo x # just string 'x'
  173.  
  174.  
  175.  
  176.  
  177.  
  178. --------------------------------------------------------------------------------
  179.  
  180. #---------------------------------------------------
  181.  
  182. # call of other programs
  183.  
  184. #---------------------------------------------------
  185.  
  186. #!/bin/sh
  187.  
  188. ls
  189.  
  190.  
  191.  
  192. --------------------------------------------------------------------------------
  193.  
  194. #---------------------------------------------------
  195.  
  196. # indirect call of other programs
  197.  
  198. #---------------------------------------------------
  199.  
  200. #!/bin/sh
  201.  
  202.  
  203.  
  204. x='pwd'
  205.  
  206. $x
  207.  
  208.  
  209.  
  210. x='ls -l'
  211.  
  212. $x
  213.  
  214.  
  215.  
  216. --------------------------------------------------------------------------------
  217.  
  218. #---------------------------------------------------
  219.  
  220. # indirect call with indirect parameters
  221.  
  222. #---------------------------------------------------
  223.  
  224. #!/bin/sh
  225.  
  226. u='-l'
  227.  
  228.  
  229.  
  230. x='ls '
  231.  
  232.  
  233.  
  234. $x $u
  235.  
  236.  
  237.  
  238. --------------------------------------------------------------------------------
  239.  
  240. #---------------------------------------------------
  241.  
  242. # print current shell name ???
  243.  
  244. #---------------------------------------------------
  245.  
  246. #!/bin/sh
  247.  
  248.  
  249.  
  250. echo $SHELL
  251.  
  252.  
  253.  
  254. --------------------------------------------------------------------------------
  255.  
  256. #---------------------------------------------------
  257.  
  258. # Anything enclosed in double quotes is passed on exactly
  259.  
  260. # as presented with the exception that the values of
  261.  
  262. # shell variables are substituted
  263.  
  264. #---------------------------------------------------
  265.  
  266. #!/bin/sh
  267.  
  268.  
  269.  
  270. v1="abc "
  271.  
  272. v2="$v1 d"
  273.  
  274. echo $v1 $v2
  275.  
  276.  
  277.  
  278. --------------------------------------------------------------------------------
  279.  
  280. #---------------------------------------------------
  281.  
  282. # Any matter enclosed in single quotes is passed on
  283.  
  284. # exactly as presented. The values of shell variables
  285.  
  286. # are not substituted.
  287.  
  288. #---------------------------------------------------
  289.  
  290. #!/bin/sh
  291.  
  292.  
  293.  
  294. v1="abc"
  295.  
  296. v2='$v1 d'
  297.  
  298. echo $v1 $v2
  299.  
  300.  
  301.  
  302. --------------------------------------------------------------------------------
  303.  
  304. #---------------------------------------------------
  305.  
  306. # Back quotes are used to enclose commands. An item
  307.  
  308. # enclosed in back quotes is replaced by the standard
  309.  
  310. # output of the command. Shell variable values
  311.  
  312. # are substituted within back quotes.
  313.  
  314. #---------------------------------------------------
  315.  
  316. #!/bin/sh
  317.  
  318.  
  319.  
  320. date=`date`
  321.  
  322. echo the date is $date
  323.  
  324.  
  325.  
  326. --------------------------------------------------------------------------------
  327.  
  328. #---------------------------------------------------
  329.  
  330. # escaping
  331.  
  332. #---------------------------------------------------
  333.  
  334. #!/bin/sh
  335.  
  336.  
  337.  
  338. msg=`echo Your Current Directory is \`pwd\``
  339.  
  340.  
  341.  
  342. echo $msg
  343.  
  344.  
  345.  
  346. --------------------------------------------------------------------------------
  347.  
  348. #---------------------------------------------------
  349.  
  350. # reading of text line from keyboard
  351.  
  352. #---------------------------------------------------
  353.  
  354. #!/bin/sh
  355.  
  356.  
  357.  
  358. read x
  359.  
  360.  
  361.  
  362. echo $x
  363.  
  364. echo $x
  365.  
  366.  
  367.  
  368. --------------------------------------------------------------------------------
  369.  
  370. #---------------------------------------------------
  371.  
  372. # reading of text line from keyboard with help comment
  373.  
  374. # echo without new line at the end
  375.  
  376. #---------------------------------------------------
  377.  
  378. #!/bin/sh
  379.  
  380.  
  381.  
  382. echo -n "Input text line=? "
  383.  
  384. read x
  385.  
  386.  
  387.  
  388. echo $x
  389.  
  390. echo $x
  391.  
  392.  
  393.  
  394. --------------------------------------------------------------------------------
  395.  
  396. #---------------------------------------------------
  397.  
  398. # syntax:  many commands in one line !!!!
  399.  
  400. #---------------------------------------------------
  401.  
  402. #!/bin/sh
  403.  
  404.  
  405.  
  406. echo "a"; echo "b"; echo "c"
  407.  
  408.  
  409.  
  410. var=5; echo `expr $var + $var`
  411.  
  412.  
  413.  
  414. --------------------------------------------------------------------------------
  415.  
  416. #---------------------------------------------------
  417.  
  418. # integer variable and its increment
  419.  
  420. # does not works in sh !!!
  421.  
  422. #---------------------------------------------------
  423.  
  424. #!/bin/bash
  425.  
  426.  
  427.  
  428. var=12345
  429.  
  430. let var=$var+1 # let is important
  431.  
  432. echo $var
  433.  
  434.  
  435.  
  436. v=12345
  437.  
  438. v=$v+1 # result "12345+1"
  439.  
  440.  
  441.  
  442. --------------------------------------------------------------------------------
  443.  
  444. #---------------------------------------------------
  445.  
  446. # integer arithmetics - bash only !
  447.  
  448. #---------------------------------------------------
  449.  
  450. #!/bin/bash
  451.  
  452.  
  453.  
  454. echo 'number=?' ; read x
  455.  
  456.  
  457.  
  458. let y=$x+$x ; echo 'x+x=' $y
  459.  
  460.  
  461.  
  462. let y=$x*$x ; echo 'square=' $y
  463.  
  464.  
  465.  
  466. let y=$x/3 ; echo 'x/3=' $y
  467.  
  468.  
  469.  
  470. let y=$x%7 ; echo 'x%7=' $y
  471.  
  472.  
  473.  
  474. --------------------------------------------------------------------------------
  475.  
  476. #---------------------------------------------------
  477.  
  478. # integer arithmetics in sh !!! using expr - slow
  479.  
  480. #---------------------------------------------------
  481.  
  482. #!/bin/sh
  483.  
  484.  
  485.  
  486. a=123
  487.  
  488. b=12
  489.  
  490.  
  491.  
  492. c=`expr $a + $b` # addition
  493.  
  494. echo $c
  495.  
  496.  
  497.  
  498. c=`expr $a \* $b` # multiplication
  499.  
  500. echo $c
  501.  
  502.  
  503.  
  504. c=`expr $a / $b` # division
  505.  
  506. echo $c
  507.  
  508.  
  509.  
  510. c=`expr $a % $b` # residual
  511.  
  512. echo $c
  513.  
  514.  
  515.  
  516. --------------------------------------------------------------------------------
  517.  
  518. #---------------------------------------------------
  519.  
  520. # very simple 'if'
  521.  
  522. #---------------------------------------------------
  523.  
  524. #!/bin/sh
  525.  
  526.  
  527.  
  528. echo 'number=?'
  529.  
  530. read x
  531.  
  532.  
  533.  
  534. if [ $x -eq 5 ]
  535.  
  536. then
  537.  
  538.     echo "five"
  539.  
  540. fi 
  541.  
  542.  
  543.  
  544. --------------------------------------------------------------------------------
  545.  
  546. #---------------------------------------------------
  547.  
  548. # if ... else
  549.  
  550. #---------------------------------------------------
  551.  
  552. #!/bin/sh
  553.  
  554.  
  555.  
  556. echo 'number=?'
  557.  
  558. read x
  559.  
  560.  
  561.  
  562. if [ $x -eq 5 ]
  563.  
  564. then
  565.  
  566.     echo "five"
  567.  
  568. else
  569.  
  570.     echo "not 5"   
  571.  
  572. fi 
  573.  
  574.  
  575.  
  576. --------------------------------------------------------------------------------
  577.  
  578. #---------------------------------------------------
  579.  
  580. # if ... elif ... else
  581.  
  582. #---------------------------------------------------
  583.  
  584. #!/bin/sh
  585.  
  586.  
  587.  
  588. echo 'number=?'
  589.  
  590. read x
  591.  
  592.  
  593.  
  594. if [ $x -eq 5 ]
  595.  
  596. then
  597.  
  598.     echo "five"
  599.  
  600. elif [ $x -eq 7 ]
  601.  
  602. then
  603.  
  604.     echo "seven"   
  605.  
  606. else
  607.  
  608.     echo "not 5 and not 7" 
  609.  
  610. fi 
  611.  
  612.  
  613.  
  614. --------------------------------------------------------------------------------
  615.  
  616. #---------------------------------------------------
  617.  
  618. # comparison -lt  and  -gt, nested if
  619.  
  620. #---------------------------------------------------
  621.  
  622. #!/bin/sh
  623.  
  624.  
  625.  
  626. echo -n 'number=?'
  627.  
  628. read x
  629.  
  630.  
  631.  
  632. if [ $x -gt 0 ]
  633.  
  634. then
  635.  
  636.     if [ $x -lt 10 ]
  637.  
  638.     then
  639.  
  640.         echo "0 < x < 10"
  641.  
  642.     fi
  643.  
  644. fi
  645.  
  646.  
  647.  
  648. --------------------------------------------------------------------------------
  649.  
  650. #---------------------------------------------------
  651.  
  652. # while loop - print first 10 integers from 0
  653.  
  654. #---------------------------------------------------
  655.  
  656. #!/bin/bash
  657.  
  658.  
  659.  
  660. x=0
  661.  
  662.  
  663.  
  664. while  [ $x -lt 10 ]
  665.  
  666. do
  667.  
  668.     echo $x
  669.  
  670.     let x=$x+1
  671.  
  672. done
  673.  
  674.  
  675.  
  676. --------------------------------------------------------------------------------
  677.  
  678. #---------------------------------------------------
  679.  
  680. # 10 random numbers generation
  681.  
  682. #---------------------------------------------------
  683.  
  684. #!/bin/bash
  685.  
  686.  
  687.  
  688. i=0
  689.  
  690. while [ $i -lt 10 ]
  691.  
  692. do
  693.  
  694.     x=$RANDOM
  695.  
  696.     echo $x
  697.  
  698.     let i=$i+1
  699.  
  700. done
  701.  
  702.  
  703.  
  704. --------------------------------------------------------------------------------
  705.  
  706. #---------------------------------------------------
  707.  
  708. # endless loop: interrupting by ctrl-c
  709.  
  710. #---------------------------------------------------
  711.  
  712. #!/bin/sh
  713.  
  714.  
  715.  
  716. while [ 1 ]
  717.  
  718. do
  719.  
  720.     read x
  721.  
  722.     echo $x$x
  723.  
  724. done
  725.  
  726.  
  727.  
  728. --------------------------------------------------------------------------------
  729.  
  730. #---------------------------------------------------
  731.  
  732. # divisors of integer number
  733.  
  734. #---------------------------------------------------
  735.  
  736. #!/bin/bash
  737.  
  738.  
  739.  
  740. echo -n 'number=?'
  741.  
  742. read x
  743.  
  744.  
  745.  
  746. i=2 # possible divisor
  747.  
  748. k=1
  749.  
  750. let n=$x/2 # top limit for divisor
  751.  
  752.  
  753.  
  754. while [ $i -le $n ]
  755.  
  756. do
  757.  
  758.     let k=$x%$i # residual
  759.  
  760.     if [ $k -eq 0 ]
  761.  
  762.     then
  763.  
  764.         echo -n "Divisor= "
  765.  
  766.         echo $i
  767.  
  768.     if
  769.  
  770.     let i=$i+1
  771.  
  772. done
  773.  
  774.  
  775.  
  776. --------------------------------------------------------------------------------
  777.  
  778. #---------------------------------------------------
  779.  
  780. # simple use of  for ... in ...
  781.  
  782. #---------------------------------------------------
  783.  
  784. #!/bin/sh
  785.  
  786.  
  787.  
  788. for i in "abc" "xyz" 1 2 99
  789.  
  790. do
  791.  
  792.     echo $i
  793.  
  794. done
  795.  
  796.  
  797.  
  798. --------------------------------------------------------------------------------
  799.  
  800. #---------------------------------------------------
  801.  
  802. # use for as in C-programming
  803.  
  804. # sum of the first n integer numbers
  805.  
  806. #---------------------------------------------------
  807.  
  808. #!/bin/bash
  809.  
  810.  
  811.  
  812. echo -n "number=?"
  813.  
  814. read n
  815.  
  816.  
  817.  
  818. s=0 # here sum
  819.  
  820.  
  821.  
  822. for((i=1; i <=n ; i++))
  823.  
  824. do
  825.  
  826.     let s=$s+$i
  827.  
  828. done
  829.  
  830.  
  831.  
  832. echo "sum= "$s
  833.  
  834.  
  835.  
  836. --------------------------------------------------------------------------------
  837.  
  838. #---------------------------------------------------
  839.  
  840. # operator case for selection of logical branches
  841.  
  842. # end marker ;; of branch
  843.  
  844. #---------------------------------------------------
  845.  
  846. #!/bin/sh
  847.  
  848.  
  849.  
  850. echo "input string=?"
  851.  
  852. read str
  853.  
  854.  
  855.  
  856. case "$str" in
  857.  
  858.     abc) echo "string = abc"
  859.  
  860.         ;;
  861.  
  862.     xyz) echo "string = xyz"
  863.  
  864.         ;;
  865.  
  866.     *)   echo "not abc, not zyz" ;;
  867.  
  868. esac
  869.  
  870.  
  871.  
  872. --------------------------------------------------------------------------------
  873.  
  874. #---------------------------------------------------
  875.  
  876. # exit operator
  877.  
  878. #---------------------------------------------------
  879.  
  880. #!/bin/sh
  881.  
  882.  
  883.  
  884. while [ 1 ]
  885.  
  886. do
  887.  
  888.     read x
  889.  
  890.     echo $x
  891.  
  892.     if [ $x -eq 0 ] # in $x must be number!
  893.  
  894.     then
  895.  
  896.         echo "script done ..."
  897.  
  898.         exit 0
  899.  
  900.     fi
  901.  
  902. done
  903.  
  904.  
  905.  
  906. --------------------------------------------------------------------------------
  907.  
  908. #---------------------------------------------------
  909.  
  910. # string comparing
  911.  
  912. #---------------------------------------------------
  913.  
  914. #!/bin/sh
  915.  
  916.  
  917.  
  918. echo "Input string=?"
  919.  
  920. read str
  921.  
  922.  
  923.  
  924. if [ $str = "abc" ]
  925.  
  926. then
  927.  
  928.     echo "You got it!"
  929.  
  930. else
  931.  
  932.     echo "Its not 'abc'"
  933.  
  934. fi
  935.  
  936.  
  937.  
  938. --------------------------------------------------------------------------------
  939.  
  940. #---------------------------------------------------
  941.  
  942. # simple strings concatenation
  943.  
  944. #---------------------------------------------------
  945.  
  946. #!/bin/sh
  947.  
  948.  
  949.  
  950. echo "Input string=?"
  951.  
  952. read str
  953.  
  954.  
  955.  
  956. s2=$str"AAAA"
  957.  
  958. echo $s2
  959.  
  960.  
  961.  
  962. s3="XXX"$s2
  963.  
  964. echo $s3
  965.  
  966.  
  967.  
  968. --------------------------------------------------------------------------------
  969.  
  970. #---------------------------------------------------
  971.  
  972. # strings concatenation
  973.  
  974. #---------------------------------------------------
  975.  
  976. #!/bin/sh
  977.  
  978.  
  979.  
  980. echo "Input string=?"
  981.  
  982. read str1
  983.  
  984.  
  985.  
  986. echo "Input second string=?"
  987.  
  988. read str2
  989.  
  990.  
  991.  
  992. s3=$str1$str2 # it works!
  993.  
  994. echo $s3
  995.  
  996.  
  997.  
  998. s4=${str1}${str2} # it works too!
  999.  
  1000. echo $s4
  1001.  
  1002.  
  1003.  
  1004. --------------------------------------------------------------------------------
  1005.  
  1006. #---------------------------------------------------
  1007.  
  1008. # testing whether a string is null
  1009.  
  1010. #---------------------------------------------------
  1011.  
  1012. #!/bin/sh
  1013.  
  1014.  
  1015.  
  1016. echo "Input string=?"
  1017.  
  1018. read str
  1019.  
  1020.  
  1021.  
  1022. if [ $str ]
  1023.  
  1024. then
  1025.  
  1026.     echo "Not empty"
  1027.  
  1028. else
  1029.  
  1030.     echo "Empty"
  1031.  
  1032. fi
  1033.  
  1034.  
  1035.  
  1036. --------------------------------------------------------------------------------
  1037.  
  1038. #---------------------------------------------------
  1039.  
  1040. # length of string
  1041.  
  1042. #---------------------------------------------------
  1043.  
  1044. #!/bin/sh
  1045.  
  1046.  
  1047.  
  1048. echo "Input string=?"
  1049.  
  1050. read str
  1051.  
  1052.  
  1053.  
  1054. leng=`expr length $str`
  1055.  
  1056. echo "length= "$leng
  1057.  
  1058.  
  1059.  
  1060. --------------------------------------------------------------------------------
  1061.  
  1062. #---------------------------------------------------
  1063.  
  1064. # how to insert string to constant string
  1065.  
  1066. #---------------------------------------------------
  1067.  
  1068. #!/bin/sh
  1069.  
  1070.  
  1071.  
  1072. var="good"
  1073.  
  1074.  
  1075.  
  1076. echo "This is $var test"
  1077.  
  1078.  
  1079.  
  1080. --------------------------------------------------------------------------------
  1081.  
  1082. #---------------------------------------------------
  1083.  
  1084. # simplest function example
  1085.  
  1086. #---------------------------------------------------
  1087.  
  1088. #!/bin/sh
  1089.  
  1090.  
  1091.  
  1092. #---------------------------
  1093.  
  1094. func()
  1095.  
  1096. {
  1097.  
  1098.     echo "Inside function"
  1099.  
  1100. }
  1101.  
  1102.  
  1103.  
  1104. #---------------------------
  1105.  
  1106. echo "Now function call..."
  1107.  
  1108. func
  1109.  
  1110. echo "end of main"
  1111.  
  1112.  
  1113.  
  1114. --------------------------------------------------------------------------------
  1115.  
  1116. #---------------------------------------------------
  1117.  
  1118. # function can see variables of main program
  1119.  
  1120. #---------------------------------------------------
  1121.  
  1122. #!/bin/sh
  1123.  
  1124.  
  1125.  
  1126. #--------------------------
  1127.  
  1128. func()
  1129.  
  1130. {
  1131.  
  1132.     echo $var
  1133.  
  1134. }
  1135.  
  1136. #--------------------------
  1137.  
  1138.  
  1139.  
  1140. var="test of global "
  1141.  
  1142. func
  1143.  
  1144.  
  1145.  
  1146. --------------------------------------------------------------------------------
  1147.  
  1148. #---------------------------------------------------
  1149.  
  1150. # pass of parameters to function
  1151.  
  1152. #---------------------------------------------------
  1153.  
  1154. #!/bin/sh
  1155.  
  1156.  
  1157.  
  1158. #---------------------------------
  1159.  
  1160. func()
  1161.  
  1162. {
  1163.  
  1164.     echo "We are in function now"
  1165.  
  1166.     echo $0  # shell script name
  1167.  
  1168.     echo $1  # first parameter
  1169.  
  1170.     echo $2  # second parameter
  1171.  
  1172.     echo "We leave function..."
  1173.  
  1174.     exit 0
  1175.  
  1176. }
  1177.  
  1178.  
  1179.  
  1180. #---------------------------------
  1181.  
  1182.  
  1183.  
  1184. func  123  "abc"
  1185.  
  1186.  
  1187.  
  1188. --------------------------------------------------------------------------------
  1189.  
  1190. #---------------------------------------------------
  1191.  
  1192. # passing variable parameters
  1193.  
  1194. #---------------------------------------------------
  1195.  
  1196. #!/bin/bash
  1197.  
  1198.  
  1199.  
  1200. #-------------------------------
  1201.  
  1202. func2()
  1203.  
  1204. {
  1205.  
  1206.     let r=$1*$1
  1207.  
  1208.     echo $r
  1209.  
  1210. }
  1211.  
  1212. #-------------------------------
  1213.  
  1214. var=123
  1215.  
  1216. func2 $var
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222. --------------------------------------------------------------------------------
  1223.  
  1224. #---------------------------------------------------
  1225.  
  1226. # recursive function example
  1227.  
  1228. # calculation of factorial
  1229.  
  1230. #---------------------------------------------------
  1231.  
  1232. #!/bin/sh
  1233.  
  1234. #-------------------------------
  1235.  
  1236. factorial()
  1237.  
  1238. {
  1239.  
  1240.     if [ "$1" -gt "1" ]
  1241.  
  1242.     then
  1243.  
  1244.         i=`expr $1 - 1`
  1245.  
  1246.         j=`factorial $i`
  1247.  
  1248.         k=`expr $1 \* $j`
  1249.  
  1250.         echo $k
  1251.  
  1252.     else
  1253.  
  1254.         echo 1
  1255.  
  1256.     fi
  1257.  
  1258. }
  1259.  
  1260. #-------------------------------
  1261.  
  1262. read x
  1263.  
  1264. factorial $x
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270. --------------------------------------------------------------------------------
  1271.  
  1272. #---------------------------------------------------
  1273.  
  1274. # using of function library ????????
  1275.  
  1276. #---------------------------------------------------
  1277.  
  1278.  
  1279.  
  1280. file with name my.lb
  1281.  
  1282.  
  1283.  
  1284. func2()
  1285.  
  1286. {
  1287.  
  1288.     echo $1$1
  1289.  
  1290. }
  1291.  
  1292. func3()
  1293.  
  1294. {
  1295.  
  1296.     echo $1$1$1
  1297.  
  1298. }
  1299.  
  1300.  
  1301.  
  1302. shell program:
  1303.  
  1304.  
  1305.  
  1306. #!/bin/sh
  1307.  
  1308.  
  1309.  
  1310. ./my.lb
  1311.  
  1312.  
  1313.  
  1314. var=123
  1315.  
  1316.  
  1317.  
  1318. func2 123
  1319.  
  1320.  
  1321.  
  1322. func3 123
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328. --------------------------------------------------------------------------------
  1329.  
  1330. #---------------------------------------------------
  1331.  
  1332. # floating point numbers
  1333.  
  1334. #---------------------------------------------------
  1335.  
  1336. #!/bin/sh
  1337.  
  1338.  
  1339.  
  1340. # does not support !!!!
  1341.  
  1342.  
  1343.  
  1344. --------------------------------------------------------------------------------
  1345.  
  1346. #---------------------------------------------------
  1347.  
  1348. # simpiest array : declaration, element access and assignment
  1349.  
  1350. #---------------------------------------------------
  1351.  
  1352. #!/bin/bash
  1353.  
  1354.  
  1355.  
  1356. arr=(aa bb cc dd)
  1357.  
  1358.  
  1359.  
  1360. echo ${arr[0]} # curly bracket notation
  1361.  
  1362. echo ${arr[1]}
  1363.  
  1364. echo ${arr[2]}
  1365.  
  1366. echo ${arr[3]}
  1367.  
  1368.  
  1369.  
  1370. arr[2]="CCCCCCC"
  1371.  
  1372. echo ${arr[2]}
  1373.  
  1374.  
  1375.  
  1376. --------------------------------------------------------------------------------
  1377.  
  1378. #---------------------------------------------------
  1379.  
  1380. # number of elements in array
  1381.  
  1382. #---------------------------------------------------
  1383.  
  1384. #!/bin/bash
  1385.  
  1386.  
  1387.  
  1388. arr=(aa bb cc dd)
  1389.  
  1390.  
  1391.  
  1392. n=${#arr[@]}
  1393.  
  1394. echo $n
  1395.  
  1396.  
  1397.  
  1398. --------------------------------------------------------------------------------
  1399.  
  1400. #---------------------------------------------------
  1401.  
  1402. # array with filenames of current directory
  1403.  
  1404. #---------------------------------------------------
  1405.  
  1406. #!/bin/sh
  1407.  
  1408.  
  1409.  
  1410. arr=(*)  # * is list of all file and dir names
  1411.  
  1412.  
  1413.  
  1414. n=${#arr[@]}
  1415.  
  1416. echo "number of files and dirs "$n
  1417.  
  1418.  
  1419.  
  1420. echo ${arr[0]}
  1421.  
  1422. echo ${arr[1]}
  1423.  
  1424.  
  1425.  
  1426. --------------------------------------------------------------------------------
  1427.  
  1428. #---------------------------------------------------
  1429.  
  1430. # print all array elements -not good
  1431.  
  1432. # works then no holes in indexes
  1433.  
  1434. #---------------------------------------------------
  1435.  
  1436. #!/bin/bash
  1437.  
  1438.  
  1439.  
  1440. arr=(aa bb cc dd ee ff gg)
  1441.  
  1442. n=${#arr[@]}
  1443.  
  1444. i=0
  1445.  
  1446. while test $i -lt $n
  1447.  
  1448. do
  1449.  
  1450.     echo ${arr[$i]}
  1451.  
  1452.     let i=$i+1
  1453.  
  1454. done
  1455.  
  1456.  
  1457.  
  1458. --------------------------------------------------------------------------------
  1459.  
  1460. #---------------------------------------------------
  1461.  
  1462. # dynamic expansion of array
  1463.  
  1464. # one array element in reality is couple (index, value)
  1465.  
  1466. #---------------------------------------------------
  1467.  
  1468. #!/bin/bash
  1469.  
  1470.  
  1471.  
  1472. arr=()
  1473.  
  1474.  
  1475.  
  1476. n=${#arr[@]}
  1477.  
  1478. echo "number of array elements "$n
  1479.  
  1480.  
  1481.  
  1482. arr[0]=a
  1483.  
  1484. n=${#arr[@]}
  1485.  
  1486. echo "number of array elements "$n
  1487.  
  1488.  
  1489.  
  1490. arr[1]=b
  1491.  
  1492. n=${#arr[@]}
  1493.  
  1494. echo "number of array elements "$n
  1495.  
  1496.  
  1497.  
  1498. arr[2]=c
  1499.  
  1500. n=${#arr[@]}
  1501.  
  1502. echo "number of array elements "$n
  1503.  
  1504.  
  1505.  
  1506. arr[10]=h
  1507.  
  1508. n=${#arr[@]}
  1509.  
  1510. echo "number of array elements "$n
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516. echo ${arr[10]}
  1517.  
  1518.  
  1519.  
  1520. echo ${arr[4]} # empty string
  1521.  
  1522. echo ${arr[6]} # empty string
  1523.  
  1524.  
  1525.  
  1526. --------------------------------------------------------------------------------
  1527.  
  1528. #---------------------------------------------------
  1529.  
  1530. # get all array and print it
  1531.  
  1532. #---------------------------------------------------
  1533.  
  1534. #!/bin/bash
  1535.  
  1536.  
  1537.  
  1538. arr=(aa bb cc dd ee ff gg)
  1539.  
  1540.  
  1541.  
  1542. echo ${arr[*]} # all array
  1543.  
  1544.  
  1545.  
  1546. echo ${arr[@]:0}   # aa bb cc dd ee ff gg
  1547.  
  1548.  
  1549.  
  1550. echo ${arr[@]:1}   # bb cc dd ee ff gg
  1551.  
  1552.  
  1553.  
  1554. echo ${arr[@]:2:3} # cc dd ee
  1555.  
  1556.  
  1557.  
  1558. for i in ${arr[*]}
  1559.  
  1560. do
  1561.  
  1562.     echo $i
  1563.  
  1564. done
  1565.  
  1566.  
  1567.  
  1568. --------------------------------------------------------------------------------
  1569.  
  1570. #---------------------------------------------------
  1571.  
  1572. # adding element to array
  1573.  
  1574. #---------------------------------------------------
  1575.  
  1576. #!/bin/bash
  1577.  
  1578.  
  1579.  
  1580. arr=(aa bb cc dd ee ff gg)
  1581.  
  1582.  
  1583.  
  1584. echo ${arr[*]}
  1585.  
  1586.  
  1587.  
  1588. arr=( "${arr[@]}" "newElem" ) # from right
  1589.  
  1590.  
  1591.  
  1592. echo ${arr[*]}
  1593.  
  1594.  
  1595.  
  1596. arr=( "newElem" "${arr[@]}" ) # from left
  1597.  
  1598.  
  1599.  
  1600. echo ${arr[*]}
  1601.  
  1602.  
  1603.  
  1604. --------------------------------------------------------------------------------
  1605.  
  1606. #---------------------------------------------------
  1607.  
  1608. # move last element from array
  1609.  
  1610. #---------------------------------------------------
  1611.  
  1612. #!/bin/bash
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618. arr=(aa bb cc dd ee ff gg)
  1619.  
  1620.  
  1621.  
  1622. echo ${arr[*]}
  1623.  
  1624.  
  1625.  
  1626. unset arr[${#arr[@]}-1] #  move last element
  1627.  
  1628. echo ${arr[*]}
  1629.  
  1630.  
  1631.  
  1632. --------------------------------------------------------------------------------
  1633.  
  1634. #---------------------------------------------------
  1635.  
  1636. # copying of array
  1637.  
  1638. #---------------------------------------------------
  1639.  
  1640. #!/bin/bash
  1641.  
  1642.  
  1643.  
  1644. arr=(aa bb cc dd ee ff gg)
  1645.  
  1646.  
  1647.  
  1648. echo ${arr[*]}
  1649.  
  1650.  
  1651.  
  1652. arr2=( "${arr[@]}" )
  1653.  
  1654.  
  1655.  
  1656. echo ${arr2[*]}
  1657.  
  1658.  
  1659.  
  1660. --------------------------------------------------------------------------------
  1661.  
  1662. #---------------------------------------------------
  1663.  
  1664. # get substring from string
  1665.  
  1666. #---------------------------------------------------
  1667.  
  1668. #!/bin/bash
  1669.  
  1670.  
  1671.  
  1672. echo "long string input=?"
  1673.  
  1674. read st
  1675.  
  1676.  
  1677.  
  1678. st2=${st:2:4}
  1679.  
  1680.  
  1681.  
  1682. echo $st2
  1683.  
  1684.  
  1685.  
  1686. --------------------------------------------------------------------------------
  1687.  
  1688. #---------------------------------------------------
  1689.  
  1690. # substring replacement "abc" to "xyz"
  1691.  
  1692. #---------------------------------------------------
  1693.  
  1694. #!/bin/bash
  1695.  
  1696.  
  1697.  
  1698. echo "string input=?"
  1699.  
  1700. read str
  1701.  
  1702.  
  1703.  
  1704. st2=${str/abc/xyz} # only ones
  1705.  
  1706.  
  1707.  
  1708. echo $st2
  1709.  
  1710.  
  1711.  
  1712. --------------------------------------------------------------------------------
  1713.  
  1714. #---------------------------------------------------
  1715.  
  1716. # search of character 'a' in a string
  1717.  
  1718. #---------------------------------------------------
  1719.  
  1720. #!/bin/sh
  1721.  
  1722.  
  1723.  
  1724. echo "string input=?"
  1725.  
  1726. read str
  1727.  
  1728.  
  1729.  
  1730. pos=`expr index $str a`
  1731.  
  1732.  
  1733.  
  1734. echo "position of the first 'a' = "$pos
  1735.  
  1736.  
  1737.  
  1738. --------------------------------------------------------------------------------
  1739.  
  1740. #---------------------------------------------------
  1741.  
  1742. # string list counting
  1743.  
  1744. #---------------------------------------------------
  1745.  
  1746. #!/bin/sh
  1747.  
  1748.  
  1749.  
  1750. for i in aa bb cc dd ee ff gg hh
  1751.  
  1752. do
  1753.  
  1754.     echo $i
  1755.  
  1756. done
  1757.  
  1758.  
  1759.  
  1760. --------------------------------------------------------------------------------
  1761.  
  1762. #---------------------------------------------------
  1763.  
  1764. # command line arguments
  1765.  
  1766. # separated by spaces
  1767.  
  1768. #---------------------------------------------------
  1769.  
  1770. #!/bin/sh
  1771.  
  1772.  
  1773.  
  1774. echo $0  # script file name
  1775.  
  1776.  
  1777.  
  1778. echo $1  # first argument
  1779.  
  1780. echo $2  # second argument
  1781.  
  1782. echo $3  # third argument
  1783.  
  1784.  
  1785.  
  1786. --------------------------------------------------------------------------------
  1787.  
  1788. #---------------------------------------------------
  1789.  
  1790. # command line arguments without script name number
  1791.  
  1792. # all command line without script name
  1793.  
  1794. #---------------------------------------------------
  1795.  
  1796. #!/bin/sh
  1797.  
  1798.  
  1799.  
  1800. echo $#  # argument number
  1801.  
  1802.  
  1803.  
  1804. echo $*  # command line
  1805.  
  1806.  
  1807.  
  1808. echo $@
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814. --------------------------------------------------------------------------------
  1815.  
  1816. #---------------------------------------------------
  1817.  
  1818. # get all files and dir names
  1819.  
  1820. #---------------------------------------------------
  1821.  
  1822. #!/bin/sh
  1823.  
  1824.  
  1825.  
  1826. echo *  # file and dir names of current dir
  1827.  
  1828.  
  1829.  
  1830. for i in *
  1831.  
  1832. do
  1833.  
  1834.     echo $i
  1835.  
  1836. done
  1837.  
  1838.  
  1839.  
  1840. echo ../* # file and dir names of parent dir
  1841.  
  1842.  
  1843.  
  1844. */ just close comments
  1845.  
  1846.  
  1847.  
  1848. --------------------------------------------------------------------------------
  1849.  
  1850. #---------------------------------------------------
  1851.  
  1852. # file search from root dir      ???????
  1853.  
  1854. # file name - parameter from command line
  1855.  
  1856. #---------------------------------------------------
  1857.  
  1858. #!/bin/sh
  1859.  
  1860.  
  1861.  
  1862. start=$HOME
  1863.  
  1864. date
  1865.  
  1866. find $start -name $1 -print
  1867.  
  1868.  
  1869.  
  1870. --------------------------------------------------------------------------------
  1871.  
  1872. #---------------------------------------------------
  1873.  
  1874. # list of all files with extension .txt     !!!!!!
  1875.  
  1876. #---------------------------------------------------
  1877.  
  1878. #!/bin/sh
  1879.  
  1880.  
  1881.  
  1882. echo *.txt
  1883.  
  1884.  
  1885.  
  1886. --------------------------------------------------------------------------------
  1887.  
  1888. #---------------------------------------------------
  1889.  
  1890. # combine a set of text files in one file use
  1891.  
  1892. # script >targetfile.lst , not txt-file !!!
  1893.  
  1894. #---------------------------------------------------
  1895.  
  1896. #!/bin/sh
  1897.  
  1898.  
  1899.  
  1900. lst=*.txt
  1901.  
  1902.  
  1903.  
  1904. for i in $lst
  1905.  
  1906. do
  1907.  
  1908.     echo
  1909.  
  1910.     echo "======================================"
  1911.  
  1912.     echo "File "$i
  1913.  
  1914.     echo "======================================"
  1915.  
  1916.     cat <$i
  1917.  
  1918. done
  1919.  
  1920.  
  1921.  
  1922. --------------------------------------------------------------------------------
  1923.  
  1924. #---------------------------------------------------
  1925.  
  1926. # create new file and write string to it
  1927.  
  1928. # file name from command string - variable $1
  1929.  
  1930. #---------------------------------------------------
  1931.  
  1932. #!/bin/sh
  1933.  
  1934.  
  1935.  
  1936. echo "String=?"
  1937.  
  1938. read str
  1939.  
  1940.  
  1941.  
  1942. echo $str >$1
  1943.  
  1944.  
  1945.  
  1946. --------------------------------------------------------------------------------
  1947.  
  1948. #---------------------------------------------------
  1949.  
  1950. # read textlines from console and add them to file
  1951.  
  1952. # file name from command string - variable $1
  1953.  
  1954. #---------------------------------------------------
  1955.  
  1956. #!/bin/sh
  1957.  
  1958.  
  1959.  
  1960. echo "Add strings=?"
  1961.  
  1962. str="1"
  1963.  
  1964.  
  1965.  
  1966. while [ $str ]
  1967.  
  1968. do
  1969.  
  1970.     read str
  1971.  
  1972.     echo $str >>$1
  1973.  
  1974. done
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980. --------------------------------------------------------------------------------
  1981.  
  1982. #---------------------------------------------------
  1983.  
  1984. # read first string from text file
  1985.  
  1986. #---------------------------------------------------
  1987.  
  1988. #!/bin/sh
  1989.  
  1990.  
  1991.  
  1992. read str <$1
  1993.  
  1994. echo $str
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000. --------------------------------------------------------------------------------
  2001.  
  2002. #---------------------------------------------------
  2003.  
  2004. # text file reading
  2005.  
  2006. # script res.txt
  2007.  
  2008. #---------------------------------------------------
  2009.  
  2010. #!/bin/sh
  2011.  
  2012.  
  2013.  
  2014. str="1"
  2015.  
  2016.  
  2017.  
  2018. while [ $str ]
  2019.  
  2020. do
  2021.  
  2022.     read str
  2023.  
  2024.     echo $str
  2025.  
  2026.     echo $str
  2027.  
  2028. done
  2029.  
  2030.  
  2031.  
  2032. --------------------------------------------------------------------------------
  2033.  
  2034.  
  2035.  
  2036. --------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment