Reputation: 2553
I have a Bash script that is working on my OpenSuSE box, but when copied across to my Ubuntu box, is not working. The script reads in from a file. The file has fields separated by white space (tabs and spaces).
#!/bin/bash
function test1()
{
while read LINE
do
if [[ $LINE =~ "^$" || $LINE =~ "^#.*" ]] ; then
continue;
fi
set -- $LINE
local field1=$1
local field2=$2
done < test.file
}
test1
with test.file containing:
# Field1Header Field2Header
abcdef A-2
ghijkl B-3
There seem to be two problems:
(1) $field2, the one with the hyphen, is blank
(2) The regex to strip out the blank lines and lines that start with # is not working
Anyone know what's wrong? As I said, it works fine on OpenSuSE.
Thanks, Paul
Upvotes: 0
Views: 794
Reputation: 26501
set
Try
while read field1 field2 dummy
do
if ! test "${field1%%#*}"
then
continue
fi
# do stuff here
done
EDIT: The obvious version using set
while read -r line
do
if ! test "${line%%#*}"
then
continue
fi
set -- $line
do_stuff_with "$@"
done
Upvotes: 3
Reputation: 3144
Apparently, as of bash 3.2 the regular expression should not be quoted. So this should work:
#!/bin/bash
while read LINE
do
if [[ $LINE =~ ^$ || $LINE =~ ^#.* ]] ; then
continue;
fi
set -- $LINE
local field1=$1
local field2=$2
done < test.file
Edit: you should probably use Jo So's answer as it's definitely cleaner. But I was explaining why the regex fails and the reason behind the different behavior between OpenSuse and Ubuntu(different version of bash, very probably)
Upvotes: 3
Reputation: 5390
On my ubuntu there is no expresion like "=~" for test
command. Just use this one:
if [[ $LINE = "" || ${LINE:0:1} = "#" ]] ; then
continue;
fi
Upvotes: -2