Unix--> 拼接文件名+变量内容并搜索

Unix--> Concatenate file name + variable content and search it

我是 shell 脚本编写的新手...我正在尝试删除一个以以前的 PID 命名的文件,但我不知道将旧 PId 连接到文件名的热点: ( 这是我的代码

...

cp ./template_dos ./user_dos.new
diff ./user_dos.new ./user_dos
if [[ $? -ne 0 ]] ; then
  if [[ -f user_dos.PID ]] ; then  #Here I search for the OLD PID->2145
  rm user_dos.PID #I would like to remove the user_dos.2145 (value old PID) I fail as i searchs for the word PID...and I want the content!
  fi
  mv user_dos  user_dos.$$ 
  mv user_dos.new user_dos
  echo $$ > PID
fi

您可以从文件中提取 PID

if [[ $? -ne 0 ]] ; then
  if [[ -f user_dos\.$(cat PID 2>/dev/null) ]] ; then  
  rm user_dos\.$(cat PID 2>/dev/null)
  mv user_dos  user_dos.$$ 
  mv user_dos.new user_dos
  echo $$ > PID
fi

我做了并且工作了

if [[ $? -ne 0 ]] ; then
  if [[ -f PID ]] ; then
    MYPID=`cat PID`
  fi
  if [[ -f user_dos.$MYPID ]] ; then
    rm user_dos.$MYPID
  fi
  mv user_dos  user_dos.$$
  mv user_dos.new user_dos
  echo $$ > PID
fi