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
To read variables from a file we can use the source or . command. You can use sed to add local keyword and make the script a bit safer and not polute your global scope. So inside a a function do cat global_variables.sh | sed -e 's/^/local /' > local_variables.sh and then use . ./local_variables.sh .
One way we can write variable contents to a file is to use the echo command along with the redirect operator. In this case, the -e argument applies to the call to echo and is not sent as output to the file. So we only see “some value”.
You're almost there:
CurPID=$(<"$PIDFile")
In the example you gave, you don't even need the temp variable. Just do:
kill -9 $(<"$PIDFile")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With