bash script input file stream - ghdrako/doc_snipets GitHub Wiki
Reading Input from Files:
Bash scripts can read data from files using commands like cat, read, or <
.
- Example using
cat
:
filename: read_file_example.sh
filename="example.txt"
while IFS= read -r line; do
echo "Line read from file: $line"
done < "$filename"
- Example using
read
:
filename: read_file_example.sh
filename="example.txt"
while read -r line; do
echo "Line read from file: $line"
done < "$filename"
Handling Input Streams
Input streams refer to data provided to a script through mechanisms like command substitution ($(command)), pipelines (|), or process substitution (<(command)).
- Example using Command Substitution:
filename: command_substitution.sh
result=$(ls -l)
echo "List of files: $result"
- Example using Pipelines:
filename: pipeline_example.sh
cat example.txt | grep "keyword"
Reading Input Line by Line
To process input line by line, read command is often used in combination with loops.
- Example:
filename: line_by_line.sh
while IFS= read -r line; do
echo "Processing line: $line"
done
Reading Specific Columns from Input
For structured data, awk or cut commands are useful for extracting specific columns.
- Example using
awk
:
filename: extract_column.sh
cat data.csv | awk -F',' '{print $2}'
- Example using
cut
:
filename: extract_column.sh
cut -d',' -f2 data.csv
Processing Input with sed and grep
sed
and grep
are powerful tools for manipulating and filtering text in input streams.
- Example using
sed
:
filename: sed_example.sh
cat data.txt | sed 's/old/new/g'
- Example using
grep
:
filename: grep_example.sh
cat log.txt | grep "error"
Redirecting Output to Files
Output from scripts can be redirected to files using >
or >>`` operators.
- Example:
filename: output_to_file.sh
echo "Hello, World!" > output.txt
Processing Multiple Files
Bash scripts can process multiple files using wildcards or loops.
- Example using Wildcards:
filename: process_files.sh
for file in .txt; do
echo "Processing file: $file"
done
Interacting with User Input
Scripts can prompt users for input using the read
command.
- Example:
filename: user_input.sh
echo "Enter your name:"
read username
echo "Hello, $username!"
Handling Binary Input
Binary input, such as images or executables, can be read using commands like hexdump
or od
.
- Example:
filename: hexdump_example.sh
hexdump -C binary_file.bin