익명 05:14

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

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

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



Top Answer/Comment:

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.

상단 광고의 [X] 버튼을 누르면 내용이 보입니다