[英]Bash: Read commands from file
I'm using a minimal netinstall image of Debian. 我正在使用Debian的最小网络安装映像。 Here is my script to start my user processes and daemons at the first login. 这是我的脚本,用于在首次登录时启动我的用户进程和守护程序。 It reads a file consisting of commands line by line, checks if they are running already and launches them, if not: 它逐行读取一个包含命令的文件,检查它们是否已在运行,如果没有,则启动它们:
#!/bin/bash
## This script reads a list of user startup daemons and launches them after the first login
STAT=`who | grep "$USER" | wc -l` ## Get login count
if [ "$STAT" -eq 1 ] ; then ## If login shell
while IFS=' ' read line ## First attempt to separate fields fails
do
CMD=`echo "$line" | grep -v ^\#` ## Use non comment lines only
if [ -n "$CMD" ] ; then
eval set "$CMD" ## The actual field separation
APPID=$(pgrep "$1")&& ## Check if process $1 of $CMD is already active and launch it if not
(echo "$1 already active as "$APPID) || ("$@" 1>/dev/null&& echo "Started $1 as $(pgrep "$1")")
fi
done < ~/.scripts/daemons
echo "start-daemons: Finished"
else
echo "start-daemons: Not a login shell"
fi
The file of commands ~/.scripts/daemons
looks as following: 命令~/.scripts/daemons
的文件如下所示:
## List of processes / daemons to start at user login
## <command> <argument1> <argument2> ..
mpd
pulseaudio -D
gpg-agent --daemon --enable-ssh-support --write-env-file /tmp/.gpg-agent-info
The script works for mpd
. 该脚本适用于mpd
。 The problem is, though, that lines consisting of a command and (multiple) options (and thus, spaces) will not be splitted into fields. 但问题是,由命令和(多个)选项(以及空格)组成的行不会拆分为多个字段。 Setting field separator IFS to ' ' makes no difference, which I find strange. 将字段分隔符IFS设置为''没什么不同,我觉得很奇怪。 What is the matter in this case? 在这种情况下怎么了? Thank you! 谢谢!
Edit: As suggested by alvits, a field separation through evaluating set $line
within the while loop, provides an easy workaround. 编辑:根据alvits的建议,在while循环中通过评估set $line
进行字段分隔提供了一种简单的解决方法。 But why does read line
no separation in the first place? 但是,为什么read line
没有分隔?
I think you should be checking as "pgrep pulseaudio" . 我认为您应该将其检查为“ pgrep pulseaudio” 。 That is, use just the process/daemon name rather than giving the process/daemon name along with the options. 也就是说,仅使用进程/守护程序名称,而不要提供进程/守护程序名称以及选项。 I don't have a machine where i can test this now. 我没有机器可以立即测试。 This you can do by taking the first field from each line(that will be the process/daemon name). 您可以通过获取每行的第一个字段来完成此操作(这将是进程/守护程序名称)。 Similarly for the second one. 第二个类似。 One more thing you can try is the "-f" option of pgrep. 您可以尝试的另一件事是pgrep的“ -f”选项。 Try n let me know 尝试n让我知道
Got it: read
needs to know the fields a line should be splitted into. 知道了: read
需要知道应将行拆分为的字段。 By changing line 6 to 通过将第6行更改为
while IFS=' ' read field1 field2
field1 will contain the command, while field2 has the options, which can contain spaces again, since anything after field1 belongs to field2. field1将包含命令,而field2具有选项,该选项可以再次包含空格,因为field1之后的任何内容都属于field2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.