How to get variable from text file into Bash variable

醉酒当歌 提交于 2019-12-03 11:39:58

问题


Simple question, in BASH I'm trying to read in a .pid file to kill a process. How do I read that file into a variable. All the examples I have found are trying to read in many lines. I only want to read the one file that just contains the PID

#!/bin/sh
PIDFile="/var/run/app_to_kill.pid"
CurPID=(<$PIDFile)

kill -9 $CurPID

回答1:


You're almost there:

CurPID=$(<"$PIDFile")

In the example you gave, you don't even need the temp variable. Just do:

kill -9 $(<"$PIDFile")



回答2:


POSIX portable way:

$ read pid <$pidfile

See: pid=`cat $pidfile` or read pid <$pidfile?



来源:https://stackoverflow.com/questions/8684447/how-to-get-variable-from-text-file-into-bash-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!