Linux:如何将文件内容作为参数传递给命令?

Linux:如何将文件内容作为参数传递给命令?

这是包裹清单文件:

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

现在我想将它作为参数传递给:

制作图像 PROFILE=TLWR740 PACKAGES=包裹清单

这是 BASH,怎么做呢?内容应该以这种方式作为参数传递:

基础文件 busybox dnsmasq dropbear 防火墙 hotplug2 iptables iptables-mod-conntrack-extra iptables-mod-filter iptables-mod-ipopt iw jshn 内核 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 replyd swconfig tc tinyproxy uboot-envtools ubus ubusd uci uhttpd wpad-mini zlib

答案1

你可以这样做:

  1. 将变量声明$packages为数组
  2. 将文件内容读packageList$packages
  3. 将所有数组元素作为列表运行 make ( ${packages[*]})

因此,此代码片段应该可以工作:

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

关于的评论${packageList[*]}。这将扩展为所有数组元素,如下所示一个壳字"one two three"。如果您在命令行中写入(请注意引号),情况也是类似的。

一个简单的例子,使用一个函数first,打印它的第一个参数

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

现在,有一个数组:

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

因此,使用@shell 会将数组拆分为多个 shell 字,而使用*它则不会。

答案2

用于tr将换行符转换为空格:

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

似乎tr只读取 stdin,所以我修改了上面的命令以通过 stdin 传递文件。

答案3

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

这样就可以了。cat打印文件内容,tr命令将换行符替换为空格。(注意:这假设它packageList是当前目录中的文件,如果它在其他地方,则需要替换名称以包含正确的路径或使用变量来引用它)

答案4

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

cat 写出文件的内容。(在 cli 中尝试。)

相关内容