0

I have a bash script

OLDIFS=$IFS
echo "Debugging: $1"
IFS=' '
frst=true
EXECPATH=$(file $1 | sed -r "s/^.*execfn: '([^']*)'.*$/\1/")
while read x id path x exec
do
    if [ $frst = true ];then
        frst=false
        path=$exec
    fi
.
.

Can someone explain what while read x id path x exec is? I know read reads the output and assign it to variables but why do I have two x?

Script is being called like ./myScrpt.sh 'ls filename'

1 Answer 1

1

Note that x is never used; it's just a dummy variable to capture fields you don't care about, so that the field splitting performed by read assigns the desired values to id, path, and exec.

_ is more commonly used as such a dummy. Also, it's better to simply override the value of IFS just for the read command than to override it globally and (attempt to) restore its value later.

echo "Debugging: $1"
frst=true
EXECPATH=$(file "$1" | sed -r "s/^.*execfn: '([^']*)'.*$/\1/")
while IFS=' ' read _ id path _ exec
do
    if [ "$frst" = true ];then
        frst=false
        path=$exec
    fi
.
.
Sign up to request clarification or add additional context in comments.

7 Comments

Whatever the input to while is; the rest of the loop probably looks something like while read ...; do ...; done <<< "$EXECPATH" (I'm assuming that the output of the pipeline is what is being parsed.)
first time when control comes to while what would it read? For ex. here(askubuntu.com/questions/604626/…) it is reading from echo and how many time while loop with read will run? infinite times?
It reads once; echo produces a single line of output, which the first call to read consumes entirely.
It looks like you want $1 to expand to multiple words (generally a bad idea; use "$@" instead of $1, and change the call to ./myScrpt.sh ls filename). In that case, file will produce multiple lines of output, and so the while loop will process each line one at a time.
what would be the value of id path and exec?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.