bash - Linux: How to pass a file content as an argument to a command?

12
2014-01
  • FernandoSBS

    this is the content of the packageList file:

    base-files
    busybox
    dnsmasq
    dropbear
    firewall
    hotplug2
    iptables
    iptables-mod-conntrack-extra
    iptables-mod-filter
    iptables-mod-ipopt
    iw
    jshn
    kernel
    kmod-ath
    kmod-ath9k
    kmod-ath9k-common
    kmod-cfg80211
    kmod-crypto-aes
    kmod-crypto-arc4
    kmod-crypto-core
    kmod-gpio-button-hotplug
    kmod-ifb
    kmod-ipt-conntrack
    kmod-ipt-conntrack-extra
    kmod-ipt-core
    kmod-ipt-filter
    kmod-ipt-ipopt
    kmod-ipt-nat
    kmod-ipt-nathelper
    kmod-leds-gpio
    kmod-ledtrig-default-on
    kmod-ledtrig-netdev
    kmod-ledtrig-timer
    kmod-lib-crc-ccitt
    kmod-lib-textsearch
    kmod-mac80211
    kmod-nls-base
    kmod-ppp
    kmod-pppoe
    kmod-pppox
    kmod-sched-connmark
    kmod-sched-core
    kmod-tun
    kmod-wdt-ath79
    libblobmsg-json
    libc
    libgcc
    libip4tc
    libip6tc
    libiwinfo
    libiwinfo-lua
    libjson
    liblua
    liblzo
    libnfnetlink
    libnl-tiny
    libopenssl
    libubox
    libubus
    libubus-lua
    libuci
    libuci-lua
    libxtables
    lua
    luci
    luci-app-firewall
    luci-app-qos
    luci-app-tinyproxy
    luci-app-upnp
    luci-i18n-english
    luci-lib-core
    luci-lib-ipkg
    luci-lib-nixio
    luci-lib-sys
    luci-lib-web
    luci-mod-admin-core
    luci-mod-admin-full
    luci-proto-core
    luci-proto-ppp
    luci-proto-relay
    luci-sgi-cgi
    luci-theme-base
    luci-theme-openwrt
    miniupnpd
    mtd
    netifd
    openvpn
    opkg
    ppp
    ppp-mod-pppoe
    qos-scripts
    relayd
    swconfig
    tc
    tinyproxy
    uboot-envtools
    ubus
    ubusd
    uci
    uhttpd
    wpad-mini
    zlib
    

    now I would like to pass it as argument to:

    make image PROFILE=TLWR740 PACKAGES=packageList

    this is BASH, how go do it? the content should be passed as argument in this way:

    base-files busybox dnsmasq dropbear firewall hotplug2 iptables iptables-mod-conntrack-extra iptables-mod-filter iptables-mod-ipopt iw jshn kernel kmod-ath kmod-ath9k kmod-ath9k-common kmod-cfg80211 kmod-crypto-aes kmod-crypto-arc4 kmod-crypto-core kmod-gpio-button-hotplug kmod-ifb kmod-ipt-conntrack kmod-ipt-conntrack-extra kmod-ipt-core kmod-ipt-filter kmod-ipt-ipopt kmod-ipt-nat kmod-ipt-nathelper kmod-leds-gpio kmod-ledtrig-default-on kmod-ledtrig-netdev kmod-ledtrig-timer kmod-lib-crc-ccitt kmod-lib-textsearch kmod-mac80211 kmod-nls-base kmod-ppp kmod-pppoe kmod-pppox kmod-sched-connmark kmod-sched-core kmod-tun kmod-wdt-ath79 libblobmsg-json libc libgcc libip4tc libip6tc libiwinfo libiwinfo-lua libjson liblua liblzo libnfnetlink libnl-tiny libopenssl libubox libubus libubus-lua libuci libuci-lua libxtables lua luci luci-app-firewall luci-app-qos luci-app-tinyproxy luci-app-upnp luci-i18n-english luci-lib-core luci-lib-ipkg luci-lib-nixio luci-lib-sys luci-lib-web luci-mod-admin-core luci-mod-admin-full luci-proto-core luci-proto-ppp luci-proto-relay luci-sgi-cgi luci-theme-base luci-theme-openwrt miniupnpd mtd netifd openvpn opkg ppp ppp-mod-pppoe qos-scripts relayd swconfig tc tinyproxy uboot-envtools ubus ubusd uci uhttpd wpad-mini zlib

  • Answers
  • mpy

    You could do it that way:

    1. Declare variable $packages as array
    2. Read content of file packageList into $packages
    3. Run make with all array elements as list (${packages[*]})

    Hence, this snippet should work:

    declare -a packages
    packages=($(< packageList))
    make image PROFILE=TLWR740 PACKAGES="${packages[*]}"
    

    A comment is appropriate about ${packageList[*]}. This expands to all array elements as one shell word. This is similar if you write "one two three" (note the quotes) on the command line.

    A simple example, using a function first, which prints its first argument:

    $ function first { echo $1 ; }
    $ first one two three
    one
    $ first "one two three"
    one two three
    

    Now, with an array:

    $ foo=(one two three)
    $ first "${foo[@]}"
    one
    $ first "${foo[*]}"
    one two three
    

    So, with @ the shell splits the array into multiple shell words, using * it does not.

  • Adrian Pronk

    Use tr to convert the newline characters to spaces:

    make image PROFILE=TLWR740 "PACKAGES=$( tr '\n' ' ' < packageList)"
    

    Seems tr only reads stdin so I've modified my command above to pass the file via stdin.

  • Michael Suelmann
    List=`cat packageList`  
    make image PROFILE=TLWR740 PACKAGES="$List"  
    

    cat writes out the content of the file. (try it in cli.)

  • SnoringFrog
    make image PROFILE=TLWR740 PACKAGES="`cat packageList | tr '\n' ' '`"
    

    That should do it. cat prints the file contents, and the tr command replaces the newlines with spaces. (Note: this assumes that packageList is a file in the current directory, if it is elsewhere you will need to replace the name to include the correct path or use a variable to reference it)


  • Related Question

    linux - bash: how to pass command line arguments containing special characters
  • Christian

    I've written myself a linux program program that needs a regular expression as input.

    I want to call the program in the bash shell and pass that regular expression as a command line argument to the program (there are also other command line arguments). A typical regular expression looks like

    [abc]\_[x|y]
    

    Unfortunately the characters [, ], and | are special characters in bash. Thus, calling

    program [abc]\_[x|y] anotheragument
    

    doesn't work. Is there a way to pass the expression by using some sort of escape characters or quotation marks etc.?

    (Calling program "[abc]\_[x|y] anotheragument" isn't working either, because it interprets the two arguments as one.)


  • Related Answers
  • U-D13

    You can either

    1. Escape each single special symbol with a backslash (as in \[abc\]_\[x\|y\]) or
    2. Doublequote the entire argument (as in "[abc]_[x|y]").

    EDIT: As some have pointed out, dobleqouting does not prevent variable expansion nor command substitution. Therefore if your regex contains something that can be interpreted by bash as one of those, use single quotes instead.

  • Flimm

    Use single quotes. Single quotes ensure that none of the characters are interpreted.

    $ printf %s 'spaces  are  not  interpreted away
    neither are new lines
    nor variable names $TESTING
    nor square brackets [TESTING]
    nor pipe characters or redirection symbols | > <
    nor the semicolon ;
    nor backslashes \a \b \c \\
    the only thing that does not work is the single quote itself
    '
    

    There are two solutions if you need to embed a single quote:

    $ printf '%s\n' '[ Don'"'"'t worry, be happy! ]'
    [ Don't worry, be happy! ]
    $ printf '%s\n' '[ Don'\''t worry, be happy! ]'
    [ Don't worry, be happy! ]
    
  • Evan Carroll

    Per man bash

    There are three quoting mechanisms: the escape character, single quotes, and double quotes.

    A non-quoted backslash () is the escape character. It preserves the literal value of the next character that follows, with the exception of . If a \ pair appears, and the backslash is not itself quoted, the \ is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

    Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

    Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, , \, and, when history expansion is enabled, !. The characters $ and retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the follow‐ ing characters: $, `, ", \, or . A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

    The special parameters * and @ have special meaning when in double quotes (see PARAMETERS below).

    Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows: \a alert (bell) \b backspace \e \E an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \ backslash \' single quote \" double quote \nnn the eight-bit character whose value is the octal value nnn (one to three digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) \cx a control-x character

    The expanded result is single-quoted, as if the dollar sign had not been present.

    A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.replacement is double-quoted.

  • Witek
    program "[abc]_[x|y]"
    program "[abc]_[x|y]" anotherargument
    
  • John T

    You can use a backslash ( \ ) in front of special characters to escape them like so:

    john@awesome:~ # echo \&
    &
  • Dennis Williamson

    Although it might not be useful as a regex, some character sequences may be interpreted as Bash variable names. To prevent this from occurring and avoid having them expanded, use single quotes instead of double quotes:

    program '[abc]_[x|y]' anotheragument
    

    Quote each argument separately (if they need quoting) so they are interpreted as independent arguments. You can also use arrays in some cases:

    param_arry=('[abc]_[x|y]' anotheragument)    # create an array
    param_arry+=(yetanother)       # append another element to the array
    program "${param_array[@]}"    # use the array elements as arguments to program
    
  • Bobby

    Escaping them should work fine:

      programm \[abc\]_\[x\|y\]
    
  • Phil P

    Where does the pattern come from? Is it fixed or from a user? Is it the user who is invoking the script on the local system, or someone remote?

    You use quotes to wrap data to keep the shell from interpreting it. There are two options:

    1. Double-quotes, which still permit some interpretation ($expand and `backticks`)
    2. Single-quotes, which pass everything through literally

    Because $ is a valid character in regexps (end-of-line/buffer) you probably want to use single-quotes to hold the regexp, unless you're storing in in a variable. If you're taking arbitrary data from someone untrusted, you'll need to replace ' with '"'"' and then wrap in single-quotes.

    Note that [abc]_[x|y] looks like you want to match x or y, while it's actually matching one of the three characters xy|. The square brackets match the characters within and only - for ranges and a ^ at the start for negation. So, [abc]_(x|y) might be what you meant, and the parentheses are the characters which are special to shell. Square-brackets are not special to shell, it just looks like they are. Double-square brackets [[ ... ]] are special.