bash files - ghdrako/doc_snipets GitHub Wiki
Bash scripts can process multiple files using wildcards or loops.
for file in *.txt; do
while read line; do
# Process each line of the file
done < "$file"
done
for file in *.txt; do
echo "Processing file: $file"
done
files=(*.txt) # Store all .txt files in the current directory
for file in "${files[@]}"; do
echo "Processing: $file"
# Add commands to process each file
done
while IFS=, read -r name email phone; do
echo "Name: $name, Email: $email"
done < contacts.csv
Bash scripts can read data from files using commands like cat, read, or <
.
filename: read_file_example.sh
filename="example.txt"
while IFS= read -r line; do
echo "Line read from file: $line"
done < "$filename"
filename: read_file_example.sh
filename="example.txt"
line_no=1
while read -r line; do
echo "Line read from file: $line"
((line_no++))
done < "$filename"
for word in $(< file); do
echo "word: $word"
done
# Create test file (not spelling out "word" to keep output < 80 columns)
IFS_TEST_FILE='/tmp/ifs-test.txt'
cat <<'EoF' > $IFS_TEST_FILE
line1 wd1 wd2 wd3
line2 wd1 wd2 wd3
line3 wd1 wd2 wd3
EoF
#--------------------------------------------------------------------------
echo 'Normal $IFS and `read` operation; split into words:'
printf '$IFS before: %q\n' "$IFS"
while read line w1 w2 w3; do
printf 'IFS during: %q\tline = %q, w1 = %q, w2 = %q, w3 = %q\n' \
"$IFS" "$line" "$w1" "$w2" "$w3"
done < $IFS_TEST_FILE
printf 'IFS after: %q\n' "$IFS"
Input streams refer to data provided to a script through mechanisms like command substitution ($(command)), pipelines (|), or process substitution (<(command)).
- Example using Command Substitution:
result=$(ls -l)
echo "List of files: $result"
- Example using Pipelines:
cat example.txt | grep "keyword"
- Reading Specific Columns from Input For structured data, awk or cut commands are useful for extracting specific columns.
cat data.csv | awk -F',' '{print $2}'
cut -d',' -f2 data.csv
grep '^nobody' /etc/passwd | { \
read -d':' user shadow uid gid gecos home shell; \
echo "$user | $shadow | $uid | $gid | $gecos | $home | $shell" \
}
nobody | x | 65534 | 65534 | nobody | /nonexistent | /usr/sbin/nologin
Binary input, such as images or executables, can be read using commands like hexdump or od.
hexdump -C binary_file.bin
mapfile is also known as readarray, but they are the same command. Added in bash v4, mapfile reads a file into an array (list) and has options to read only -n count lines at a time, skip -s count lines, display a progress indicator (-c/-C), and more. This is much easier to use than other methods.
mapfile -t nodes < /path/to/list/of/hosts # -t removes newlines
# Loop around the nodes
for node in ${nodes[@]}; do
ssh $node 'echo -e "$HOSTNAME:\t$(uptime)"'
done