简体   繁体   English

Bash中的“欺骗”文件扩展名

[英]“Spoof” File Extension In Bash

Is there a way to "spoof" the file extension of a file in bash for consumption by another program? 有没有办法在bash中“欺骗”文件的文件扩展名以供其他程序使用? I can think of doing some shell scripting and making lots of soft-links, but that isn't very scalable. 我可以考虑做一些shell脚本并制作大量的软链接,但这不是很容易扩展。

Let's imagine I have a program I'm trying to use that requires input files to be of a specific file extension, and it has no method of turning off this check. 让我们假设我有一个我正在尝试使用的程序,它要求输入文件具有特定的文件扩展名,并且它没有关闭此检查的方法。

You could make a fifo with the requisite extension and cat any other file type into it. 你能与必要的延伸和FIFO cat任何其他文件类型到它。 So, if your crazy program needs to see files that end in .funky , you can do this: 所以,如果你的疯狂程序需要查看以.funky结尾的文件,你可以这样做:

mkfifo file.funky
cat someotherfile > file.funky &
someprogram file.funky

Create a symbolic link for each file you want to have a particular extension, then pass the name of the symlink to the command. 为要具有特定扩展名的每个文件创建符号链接,然后将符号链接的名称传递给该命令。

For example suppose you have files with names of the form *.foo and you need to refer to them with extensions of .bar : 例如,假设您的文件名称为*.foo并且您需要引用扩展名为.bar

for file in *.foo ; do
    ln -s $file _$$_$file.bar
done

I precede each symlink name with _$$_ to avoid the possibility of colliding with an existing file name (you don't want to do ln -s file.foo file.bar if file.bar already exists). 我在每个符号链接名称前面加上_$$_以避免与现有文件名冲突的可能性(如果file.bar已经存在,则不想执行ln -s file.foo file.bar )。

With a little more programming, your script can keep track of which symlinks it created and, if you like, clean them up after executing the command. 通过稍微编程,您的脚本可以跟踪它创建的符号链接,如果您愿意,可以在执行命令后对其进行清理。

This assumes, as you stated in the question, that the command can't be forced to accept a different extension. 正如您在问题中所述,这假定命令不能强制接受不同的扩展。

You could, without too much difficulty, create a wrapper script that replaces the command in question, creating the symlinks, invoking the command, and cleaning up after itself automatically. 您可以毫不费力地创建一个包装器脚本来替换相关命令,创建符号链接,调用命令以及自动清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM