Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Confused about splitting
- X-seq: zsh-users 23893
- From: Anssi Saari <as@xxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Confused about splitting
- Date: Sun, 31 Mar 2019 12:06:16 +0300
- Cancel-lock: sha1:4vjDPzPl/oApSiMH9YBxqQtxt+U=
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- Organization: An impatient and LOUD arachnid
I have a simple script which uses awk to assign and split a line from a
file to a zsh array. Simplified, just this:
foofunc () {
if [[ $# -ge 1 ]]
then
if=$1
else
if=eno1
fi
first=(`awk /$if/ /proc/net/dev`)
echo $first
echo length of first is $#first
}
Obvious and works, it prints out currently with parameter eno1 this:
eno1: 44977581223 33469291 0 139 0 0 0 57718 12574512832 21508919 0 0 0 0 0 0
length of first is 17
/proc/net/dev currently contains
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 990081491 293912 0 0 0 0 0 0 990081491 293912 0 0 0 0 0 0
eno1: 44978021588 33472121 0 139 0 0 0 57964 12575022205 21512486 0 0 0 0 0 0
But wanting to not call awk I ran into trouble.
If I set shwordsplit, no problem:
foofunc2 () {
if [[ $# -ge 1 ]]
then
if=$1
else
if=eno1
fi
setopt shwordsplit
while read g
do
if [[ $g =~ $if ]]
then
first=($g)
break
fi
done < /proc/net/dev
echo $first
echo length of first is $#first
}
But what if I don't want to set shwordsplit? I came up with
foofunc3 () {
if [[ $# -ge 1 ]]
then
if=$1
else
if=eno1
fi
while read g
do
if [[ $g =~ $if ]]
then
first=${=g}
break
fi
done < /proc/net/dev
echo $first
echo length of first is $#first
}
But this splits each character into an array element. I don't understand
this behavior at all. As I understand it, = in the assignment to $first
is supposed to turn shwordsplit on temporarily but it really doesn't
seem to. IFS is not set so field separator should be default, meaning
whitespace.
This is with zsh 5.3.1 on Debian.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author