Skip to main content
added 23 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

list=$1listfile=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$list<$listfile

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a separatebash array to hold the command line arguments that we create:

#!/bin/bash

list=$1listfile=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$list<$listfile

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).

Both scriptscripts above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.

Related:

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

list=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$list

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a separate array to hold the command line arguments that we create:

#!/bin/bash

list=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$list

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces or filename globbing characters).

Both script above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.

Related:

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

listfile=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$listfile

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a bash array to hold the command line arguments that we create:

#!/bin/bash

listfile=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$listfile

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).

Both scripts above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.

Related:

added 92 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

list=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$list

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a separate array to hold the command line arguments that we create:

#!/bin/bash

list=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$list

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces or filename globbing characters).

Both script above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.

Related:

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

list=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$list

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a separate array to hold the command line arguments that we create:

#!/bin/bash

list=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$list

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces or filename globbing characters).

Both script above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

list=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$list

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a separate array to hold the command line arguments that we create:

#!/bin/bash

list=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$list

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces or filename globbing characters).

Both script above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.

Related:

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.

Here's a solution for /bin/sh:

#!/bin/sh

list=$1

set --
while IFS= read -r line; do
    set -- "$@" -o "$line:1"
done <$list

util "$@"

This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.

After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.

With bash and using a separate array to hold the command line arguments that we create:

#!/bin/bash

list=$1

while IFS= read -r line; do
    args+=( -o "$line:1" )
done <$list

util "${args[@]}"

In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces or filename globbing characters).

Both script above would be used as

$ ./script.sh file

where script.sh is the executable script file and file is the input file name to read from.