22
33# All provided values are evaluated in while cycles...
44while getopts " hvi:o:a:" INITARGS; do
5- case " $INITARGS " in # $INITARGS contains the parameters to evaluate
5+ case " ${ INITARGS} " in # $INITARGS contains the parameters to evaluate
66 h|v) # Accept parameters "-h" or "-v" for help
77 echo " The script reads input text file and writes it given number of times into output text file." # What will be done it this is selected...
88 echo -e " \t\" -h\" or \" -v\" for this help"
@@ -13,19 +13,19 @@ while getopts "hvi:o:a:" INITARGS; do
1313 exit # Terminate after providing help
1414 ;; # End of this option
1515 i) # Parameter "-i" accepts some value (e.g. "-i inputfile.txt")
16- if [ -r " $OPTARG " ]; then # Check if the input file exists and is readable
17- echo " OK, File \" $OPTARG \" exists and is readable. Proceeding..."
18- INPUTFILE=" $OPTARG " # $OPTARG always contains value of parameter
16+ if [ -r " ${ OPTARG} " ]; then # Check if the input file exists and is readable
17+ echo " OK, File \" ${ OPTARG} \" exists and is readable. Proceeding..."
18+ INPUTFILE=" ${ OPTARG} " # $OPTARG always contains value of parameter
1919 else
20- echo " Error! File \" $OPTARG \" doesn't exist or isn't readable!"
20+ echo " Error! File \" ${ OPTARG} \" doesn't exist or isn't readable!"
2121 echo
2222 exit 1
2323 fi
2424 ;; # End of this option
2525 o) # Parameter "-o" accepts some value (e.g. "-o outputfile.txt")
26- if [ -n " $OPTARG " ]; then # Check if there is some name for output variable
27- echo " OK, Name for output file was provided: \" $OPTARG \" . Proceeding..."
28- OUTPUTFILE=" $OPTARG " # $OPTARG always contains value of parameter
26+ if [ -n " ${ OPTARG} " ]; then # Check if there is some name for output variable
27+ echo " OK, Name for output file was provided: \" ${ OPTARG} \" . Proceeding..."
28+ OUTPUTFILE=" ${ OPTARG} " # $OPTARG always contains value of parameter
2929 else
3030 echo " Error! No output file name provided!"
3131 echo
@@ -34,15 +34,15 @@ while getopts "hvi:o:a:" INITARGS; do
3434 ;; # End of this option
3535 a) # Parameter "-a" accepts some value (e.g. "-a X" for number)
3636 # Check if provided value makes sense (integer between 10 and 300)
37- if [[ " $OPTARG " =~ ^[0-9]+$ ]] && [ " $OPTARG " -ge 10 ] && [ " $OPTARG " -le 300 ]; then # The condition is long...
38- VALUE=$OPTARG # $OPTARG always contains value of parameter
39- echo " Value is OK: $VALUE "
37+ if [[ " ${ OPTARG} " =~ ^[0-9]+$ ]] && [ " ${ OPTARG} " -ge 10 ] && [ " ${ OPTARG} " -le 300 ]; then # The condition is long...
38+ VALUE=${ OPTARG} # $OPTARG always contains value of parameter
39+ echo " Value is OK: ${ VALUE} "
4040 else
4141 echo " Error! For parameter \" -a\" you did not provide an integer ranging from 10 to 300!"
4242 exit 1
4343 fi
4444 ;; # End of this option
45- ? )
45+ * )
4646 echo " Invalid option(s)!"
4747 echo " See \" $0 -h\" for usage options."
4848 exit 1
5252
5353# Check if all required values are provided
5454
55- if [ -z " $INPUTFILE " ] || [ -z " $OUTPUTFILE " ]; then
55+ if [ -z " ${ INPUTFILE} " ] || [ -z " ${ OUTPUTFILE} " ]; then
5656 echo " Error! Name of input and/or output file was not provided!"
5757 echo " See \" $0 -h\" for help usage..."
5858 echo
5959 exit 1
6060 fi
6161
62- if [ -z " $VALUE " ]; then
62+ if [ -z " ${ VALUE} " ]; then
6363 echo " Warning! Value for \" -a\" was not provided! Using default value of 10."
6464 VALUE=10
6565 fi
6666
6767# Do the job...
68- for I in $( seq 1 $ VALUE) ; do # Repeat task given number of times ($VALUE x)
69- echo -ne " Cycle $I ...\r" # Write number of cycle and return cursor to the beginning of the line to overwrite the number in next step
68+ for I in $( seq 1 " ${ VALUE} " ) ; do # Repeat task given number of times ($VALUE x)
69+ echo -ne " Cycle ${I} ...\r" # Write number of cycle and return cursor to the beginning of the line to overwrite the number in next step
7070 sleep 1s # Wait 1 second - just for fun ;-)
71- cat " $INPUTFILE " >> " $OUTPUTFILE " # Do the task - append input to the output - note usage of variables
71+ cat " ${ INPUTFILE} " >> " ${ OUTPUTFILE} " # Do the task - append input to the output - note usage of variables
7272 done
7373
7474echo -ne " \n" # Reset cursor to new line
0 commit comments