Can a shell script delete or overwrite itself?

前端 未结 2 945
野的像风
野的像风 2021-01-11 19:37
#beginning of bashscript1.sh
source bashscript2.sh
echo $variable

and here is the source file:

#beginning of bashscript2.sh
rm -f b         


        
2条回答
  •  不思量自难忘°
    2021-01-11 20:06

    Is this a curiosity you have or what you actually want to obtain?

    Anyway, I've tried myself this simple script (test_script.sh):

    rm -f test_script.sh
    echo "Still here!"
    

    and it prints Still here!, so the file was loaded before execution. Execution is not possible the second time.

    To answer your question, yes and no. The commands in the script are loaded and executed, and can delete the source file of the script. This will make impossible to execute the same file again, of course, but will not affect the first execution.

提交回复
热议问题