Bash:保存一个文件';s当前目录,并将其与此文件关联,而;查找“;这是假的吗?

Bash:保存一个文件';s当前目录,并将其与此文件关联,而;查找“;这是假的吗?,bash,Bash,我想制作一个脚本,在Makefile中添加一些文件 我想保存从Makefile到文件的相对路径 我想做点像这样的事 file=$1 find Makefile while [ $? -eq 1 ] do ##"save current directory" ## ##"concatanate to $file" ## cd -- done 您可以执行以下操作: cd $1 while true; do if [ ! -f Makefile ]; then return

我想制作一个脚本,在Makefile中添加一些文件

我想保存从Makefile到文件的相对路径

我想做点像这样的事

file=$1
find Makefile
while [ $? -eq 1 ]
do
  ##"save current directory" ##
  ##"concatanate to $file" ##
  cd --
done

您可以执行以下操作:

cd $1
while true; do
  if [ ! -f Makefile ]; then
    return 0
  fi
  echo $PWD
  cd ..
done

这将转到arg目录并显示目录,直到找到一个没有Makefile的目录

我正试图以另一种方式获得这个相对路径

res=$(readlink -f $1)
echo "path of my file = $res"
find Makefile 2> /dev/null
while [ $? -eq 1 ]
do
    cd ..
    find Makefile 2> /dev/null
done
res2=$(pwd)
echo "$res2 is the Makefile path"
res=$(sed -i "s/$res2/.//g" <<< $res)
echo "the path becomes $res"
我的上一个sed不起作用,因为我的变量包含“/”,而我的第一个sed的技巧对它不起作用。我是shell编程的初学者,这个问题似乎很容易解决。我在找我自己来修。
再次感谢

请提供一个文件/目录结构示例和所需的输出
,而
非常可怕。您似乎想要
while[!-f./Makefile];做cd。。;完成
与您收到的另一个答案大致相同。您正在寻找
查找-键入f-名称Makefile>>“$1”
?现在还不清楚您想在这里完成什么,而没有任何用处的代码并不是传达您想要的信息的好方法。我希望有Makefile和我的文件之间的相对路径,例如,如果我在project/src/error/中为“toto.c”运行代码,我希望有“project/src/error”因为我的Makefile在/project中。最后一个建议是在我的Makefile中添加“project/src/error/toto.c”如下所示
res=$(readlink -f $1)
echo "path of my file = $res"
while [ ! -f ./Makefile ]
do
    cd ..
done
res2=$(pwd)

echo "$res2 is the Makefile path"

res=$(echo "$res" | sed s\#"$res2"\#\#g)

echo "the path becomes $res"

VAR="\n\t\t\t$res\t\t\t\t\t\t\\\\"
sed -i -e "s/^SRC.*$/&$VAR/g" Makefile
printf "$1 added in Makefile\n"