Friday, April 1, 2016

Linux Bash Shell Script Command Line Argument and Read Line Data in Separate Variables


How the code below works:

The first code reads the file line by line that was passed in as the first argument.
The second code reads line into three variables where in each line there are three string separated by space.

Code To Read Argument File From Terminal line by line:

Save the code in a file named "TerminalArgumentFileRead.sh".

Running the Code:

Just type the code below in terminal. Make sure to cd into that directory if you have not opened the terminal in that directory. Make sure to create the "log.txt" file in the same directory as the shell script file.

bash TerminalArgumentFileRead.sh log.txt

Code To Read Argument File From Terminal in Three Variables:

Save the code in a file named "ReadLineInThreeVariables.sh".

Running the Code:

Just type the code below in terminal. Make sure to cd into that directory if you have not opened the terminal in that directory. Also make sure the argument file you are passing exist and formatted in the aforementioned way.

bash ReadLineInThreeVariables.sh log.txt

Sample Input File:

Save as "log.txt".

No comments: