Linux Echo and Reading Files
Assigning values to variables, using ECHO and reading a file.
Assigning Variables
Name the variable on the left. Variables names are case sensitive.
No spaces around the equals sign in the middle.
The value is on the right side of the equals sign.
When using the variable afterwards, precede the variable name with a $.
-e will allow interpretation of switches. Use double-quotes around variables after. \n is a new line.
$ testVars=valueHere $ echo $testVars valueHere $ echo $testvars <nothing here> $echo -e "$testVars\n$testVars"
Substring of a Variable
With this example, the echo outputs testVars starting at position 7 for 2 characters. The next is showing a substring of testVars starting at position 8. Since no length is specified, it returns the string to the end.
$ testVars=abcdefghijklmnopqrstuvwxyz $ echo ${testVars:7:2} hi $ echo ${testVars:8} ijklmnopqrstuvwxyz
While Loops
while [ condition ] do [command] done or while [condition]; do [command]; done
Read a File Line by Line
The following commands read each line of the file indicated (file.csv). The I/O descriptor < is used to indicate the contents of file.csv should be an input for this line of code.
# while read line; do echo -e "$line\n"; done < file.csv
Using the substring above, we can parse out only a piece of the line. Also, send the information retrieved into another file. The I/O descriptor > is used to indicate the contents of file.csv as parsed out by the line of code should be output to file.tmp.
$ while read line; do echo ${line:1:33}; done <file.csv >file.tmp