Bash : Parameter expansion ${var:-file*}

Example data

$ ls *somehost*
10.10.10.10_somehost1.xyz.com.log  
11.11.11.11_somehost2.xyz.com.log
#!/bin/bash
#FILES="*.log"
FILES=${FILES:-*.log}

for x in $FILES
do
ip="${x%%_*}"     # isolate IP address
x="${x##*_}"       # isolate hostname
hnam="${x%.*}" # Remove the ".log" extension so that only the hostname.xyz.com remains
echo "$ip $hnam"

Output

10.10.10.10 somehost1.xyz.com
11.11.11.11 somehost2.xyz.com

Questions

  1. Which is faster and more proper FILES="*.log" or FILES=${FILES:-*.log} or something else. There may be a very large number of files I would want to loop through.
  2. How can I combine the last two parameter expansion statements : x="${x##*_}" and hnam="${x%.*}" without external calls.

Thanks in advance.
Pop

If you use the variable FILES, then when when you expand it, it becomes susceptible to field splitting by the shell and that can break your script if there are files that contain spaces..

Either use:

for x in *_*.log
do

Or, since this is bash you can also use arrays):

FILES=( *_*.log )
for x in "${FILES[@]}"; 
do

I don't think it makes much difference in speed between those approaches..

You can also do something like this in the loop:

IFS=_ read ip hnam <<< "${x%.log}"

You can also do this:

FILES=( *_*.log )
for x in "${FILES[@]%.log}"; 
do
  IFS=_ read ip hnam <<< "$x"
  ...
done

--
Depending on what you need to do and if you can operate on multiple files at ones, it may be more benificial, speedwise to use a find or an xargs construct..

1 Like

thanks ..

Moderator comments were removed during original forum migration.