简体   繁体   中英

Bash script with regex not behaving on Ubuntu

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

  1. Quoting is wrong, that probably accounts for the regex failing.
  2. No need to use bashisms.
  3. No need to use 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

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)

On my ubuntu there is no expresion like "=~" for test command. Just use this one:

if [[ $LINE = "" || ${LINE:0:1} = "#" ]] ; then
    continue;
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM