从自身内部删除MATLAB脚本是否安全? [英] Is it safe to delete a MATLAB script from within itself?

查看:131
本文介绍了从自身内部删除MATLAB脚本是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我们可以将以下内容放置在名为me.m的脚本中.

In MATLAB, we could place the following inside of a script named me.m.

delete('me.m');

运行脚本后,它会自行删除.在MATLAB中这样安全吗?

After we run the script, it removes itself. Is such a thing safe in MATLAB?

推荐答案

脚本在调用时由MATLAB编译,编译后的脚本加载到内存中,然后从内存中运行.类,函数,脚本和MEX文件都是如此.您可以使用 inmem 来获取包含以下内容的所有源文件的列表:当前存储在内存中.

The script is compiled by MATLAB when you call it, the compiled script is loaded into memory, and then run from memory. This is true of classes, functions, scripts, and MEX files. You can use inmem to get a list of all source files that are currently stored in memory.

如果您从脚本中删除源文件,它仍将完成(因为它使用的是内存中版本),但是显然将无法再次运行.

If you delete the source file from within the script, it will still complete (because it is using the in-memory version), but obviously would not be available to be run again.

您可以通过将其粘贴在脚本中来亲自查看

You can see this for yourself by sticking this in a script

%// Prove that we started
disp('About to self destruct')

%// Get the name of the current script
this = mfilename;

%// Remove the source file
delete(which(this))

%// Make sure we actually removed it
if exist(which(this))
    disp('Not deleted')
else
    disp('File is gone!')
end

%// Check that it is still in memory
if any(ismember(this, inmem))
    disp('Still in memory')
else
    disp('Not found in memory')
end

%// Demonstrate that we still execute this
disp('I am unstoppable')

如果您再次尝试运行此脚本,将找不到该脚本.

If you then try to run this script again, it will not be found.

关于存储在存储器中的功能,脚本等.您始终可以使用clear明确清除它们或清除内存中的特定类型.

With regards to functions, scripts, etc. being stored in memory. You can always use clear to explicitly clear them out or to clear everything of a specific type from memory.

%// Clear out an explicit script
clear scriptname

%// Clear all functions & scripts
clear functions

有趣的是,即使您从脚本scriptname.m中调用clear scriptname,也不会阻止脚本完成.

Interestingly, even if you call clear scriptname from within the script scriptname.m, this will not prevent the script from completing.

%// Get the name of the script
this = mfilename;

%// Delete the file
delete(which(this));

%// Try to clear this script from memory
clear(this);

%// Prove that we're still running
disp('Still here')

另一个有趣的花絮是 mlock 旨在防止即使当前执行中的功能/脚本完成后也不会从内存中删除.如果将其插入到脚本中(删除文件后),脚本完成后,脚本 still 将使用inmem 出现,但是,您仍然无法调用再次找到该脚本,因为找不到源文件.

Another interesting tidbit is that mlock is intended to prevent the currently executing function/script from being removed from memory even after it completes. If you insert this into the script (after deleting the file), the script still shows up using inmem after the script completes, however, you still can't call the script again since the source file cannot be found.

this = mfilename;
delete(which(this));
mlock
disp('Still here')

然后在命令窗口中

%// Run the self-destructing script
scriptname

%// Check to see if it is in memory
ismember('scriptname', inmem)

%// Now try to run it again (will not work)
scriptname

摘要

那么从自身内部删除脚本安全是否安全? .似乎您无法通过删除源文件来阻止当前正在执行的脚本运行完毕.

Summary

So is it safe to delete a script from within itself? Yes. It seems as though you cannot prevent a currently executing script from running to completion by deleting the source file.

这篇关于从自身内部删除MATLAB脚本是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆