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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash : Parameter expansion ${var:-file*}
# 1  
Old 10-12-2019
Bash : Parameter expansion ${var:-file*}

Example data
Code:
$ ls *somehost*
10.10.10.10_somehost1.xyz.com.log  
11.11.11.11_somehost2.xyz.com.log

Code:
#!/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
Code:
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

Old 10-12-2019
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:
Code:
for x in *_*.log
do

Or, since this is bash you can also use arrays):
Code:
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:
Code:
IFS=_ read ip hnam <<< "${x%.log}"

You can also do this:
Code:
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..

Last edited by Scrutinizer; 10-12-2019 at 07:10 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-15-2019
thanks ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. Shell Programming and Scripting

Bash : More parameter expansion and IFS

I am trying to become more fluent with the interworking of bash and minimize the number of external calls. Sample Data. This will be the response of the snmp query. SNMPv2-MIB::sysName.0 = STRING: SomeHostName SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1.1745... (5 Replies)
Discussion started by: sumguy
5 Replies

3. Shell Programming and Scripting

Bash Parameter Expansion

#!/bin/bash SNMPW='/usr/bin/snmpwalk' while read h i do loc=$($SNMPW -v3 -u 'Myusername' -l authPriv -a SHA -A 'Password1' -x AES -X 'Password2' $i sysLocation.0 2>/dev/null) loc=${loc:-" is not snmpable."} loc=${loc##*: } loc=${loc//,/} echo "$i,$h,$loc" done < $1 My question is ... ... (1 Reply)
Discussion started by: sumguy
1 Replies

4. Shell Programming and Scripting

Bash Parameter Expansion

I have made the following examples that print various parameter expansions text: iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.250.070018.sac (text%.*): iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.250.070018 (text%%.*): iv-hhz-sac/hpac/hhz (text#*.): d/iv.hpac..hhz.d.2016.250.070018.sac... (2 Replies)
Discussion started by: kristinu
2 Replies

5. UNIX for Dummies Questions & Answers

Parameter Expansion with regular expression

Hello experts, I am exploring parameter expansion, and trying to cut the fields in a URL. Following is the requirement: I have // abc.nnt /dir1/dir2/dir3/dir4/somefile.java What i need to get is the path after dir3, and dir3 will be passed. output that i need is... (1 Reply)
Discussion started by: gjarms
1 Replies

6. Shell Programming and Scripting

Parameter expansion not working for all strings...

I'm trying to write a script that parses my music collection and hard link some filenames that my media player doesn't like to other names. To do this I need to extract the name and remove alla non ASCII characters from that and do a cp -l with the result. Problem is this: 22:16:58 $... (8 Replies)
Discussion started by: refuser
8 Replies

7. Shell Programming and Scripting

Bash parameter expansion from a config file

Hi - I am trying to do a simple config file with known variable names in it, e.g.: contents of config file a.conf: -a -b $work -c $host simplified contents of bash script file: work='trunk' host='alaska' opts=$(tr '\n' ' ' < a.conf) opts="$opts $*" mycommand $opts arg1 arg2 The... (3 Replies)
Discussion started by: mrengert
3 Replies

8. Shell Programming and Scripting

strip the square brackets from a word using sed or var expansion

Hi, I am trying to strip out leading and trailing brackets from a word. for example, I have a value, in a file. I want to strip out the leading and trailing brackets it and should get the value Running. I am using the following statement but in vain. grep "Workflow run status:" <... (4 Replies)
Discussion started by: svajhala
4 Replies

9. Shell Programming and Scripting

Need help with parameter expansion

Say you have this numeric variable that can be set by the user but you never want it to leave a certain range when it gets printed. How could you use parameter expansion such that it will never expand outside of that boundary? Thanks ---------- Post updated at 11:09 PM ---------- Previous update... (3 Replies)
Discussion started by: stevenswj
3 Replies

10. Shell Programming and Scripting

removing html tags via parameter expansion

Hi all- I have a variable that contains a web page: echo $STUFF <html> <head> <title>my page</title></head> <body> blah blah etc.. Can I use the shell's parameter expansion abilities to remove just the tags? I thought that FIXHTML=${STUFF//<*>/} might do it, but it didn't seem to... (2 Replies)
Discussion started by: rev66
2 Replies
Login or Register to Ask a Question