Saturday, March 5, 2016
BASH Shell Script Calculate Factorial by Reading From File and Passing data to Another script to Calculate
March 05, 2016
bash for loop
,
expression evaluation
,
factorial calculation
,
input from terminal
,
linux
,
operating system
,
os
,
passing script data to another script
,
read command line argument
,
read from file
,
shell scripting
BASH Shell Scripts Run order:
</> List:cleanFile.sh numberGenerator.sh factorialMain.sh factorial.sh
Details:
cleanFile.sh deletes all the contents of the file passed in the arguments. numberGenerator.sh generates numbers based on the code and writes to specified file. factorialMain.sh reads the file created by numberGenerator.sh line by line and passed the data read to factorial.sh to calculate and print result. factorial.sh is a standalone script. It can either use command line arguments passed to calculate factorial or if no arguments was passed then asks to enter a number then prints its factorial. These scripts can be modified to calculate other things or do file related tasks etc.Codes:
cleanFile.sh
numberGenerator.sh
factorialMain.sh
factorial.sh
Run Instruction:
Run each file by first bringing up terminal ( Shortcut: ctrl + alt + t ), then type the following,
bash scriptName.shReplace scriptName.sh with the actual script name such as, "factorialMain.sh". Also make sure to bring up terminal in the folder where the script is located, or cd to that directory then run the command. In order to pass command line arguments just call the script as above and add space separated numbers to it. For example,
bash factorial.sh 5It will print the factorial of 5 which is 120.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment