存档重新生成Shell脚本 [英] archive regenerating shell script

查看:50
本文介绍了存档重新生成Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个生成特殊归档"的shell程序.这些归档文件也将是Shell脚本,因此可以使用"sh"执行.

I need to create a shell program which generates special "archives". These archives will also be shell scripts, so that they can be executed with "sh ".

假设我有一些目录结构,例如:

Let's say I have some directory structure like:

mydir
 +- file.txt
  +- mysubdir
   +- foo.txt

file.txt包含"lorem ipsum",而foo.txt包含"foo bar".

file.txt contains "lorem ipsum" and foo.txt contains "foo bar".

然后执行存档程序:

$ arch.sh mydir> bundle.sh

$ arch.sh mydir > bundle.sh

应生成一个名为"bundle.sh"的文件,该文件可能类似于以下内容:

Should generate a file named "bundle.sh" which may look something like this:

if [ \! -d mydir ] ; then
mkdir mydir
cd mydir
if [ -f file.txt ] ; then
touch file.txt
echo "lorem ipsum" >> file.txt
fi
if [ \! -d mysubdir ] ; then
mkdir mysubdir
cd mysubdir
if [ -f foo.txt ] ; then
  touch foo.txt
  echo "foo bar" >> foo.txt
fi
fi
fi

因此,基本上,如果您将执行此捆绑包脚本,那么最终将获得与生成捆绑包时完全相同的目录结构和文件内容.现在,只需要写一个遍历所有文件和目录并创建的程序每个输出的适当输出.对于文本文件,您可以对其进行处理使用sed将文本文件中的每一行都转换为shell命令.例如,如果文件如下所示:

So, basically, if you would execute this bundle script then you would end up with exactly the same directory structure and file contents that you had when you generated the bundle. Now, only need to write a program which iterates over all the files and directories and creates the appropriate output for each. For text files, you could process them with sed to convert every line in the text file to a shell command. For example, if the file looks like this:

Foo
Bar
Baz
Goo

然后,您可以生成以下shell命令:

Then you could generate the following shell commands:

echo "Foo" >> example.txt
echo "Bar" >> example.txt
echo "Baz" >> example.txt
echo "Goo" >> example.txt

对于二进制文件,您应该使用"uuencode",或者也可以使用"base64",例如当您有一个二进制文件"file.bin"时,执行以下命令输出一个Shell脚本,该脚本重新创建二进制文件:

For binary files, you should use "uuencode" or maybe you can also use "base64", e.g. when you have a binary file "file.bin" then executing the following commands output a shell script which recreates the binary file:

base64 file.bin | sed 's/.*/echo\ \0 >> file.txt/g'
echo "base64 -d file.txt > file.bin"
echo "rm file.txt"

因此,我可以通过电子邮件将bundle.sh命令发送给朋友,然后当他在计算机上执行bundle.sh时,他将生成与mydir完全相同的目录结构.

So in this way, I can just email the bundle.sh commands to a friend, and then when he executes bundle.sh on his machine, he will generate exactly the same directory structure as, say, mydir.

我想知道如何进行递归遍历目录,如何获取每个文件的名称,以及如何将我生成的所有shell命令捆绑到bundle.sh.谢谢.伙计们.

I want to know how to do the recursion to traverse the directory and how to get the name of each file and how to bundle all the shell commands that I generated to bundle.sh. Thanks. guys.

推荐答案

使用 makeself :

$ cd /path/to/dir
$ ls 
main.sh dir1 dir2 dir3
$ makeself . ../bundle.run "Creating a bundle with makeself" ./main.sh

main.sh 是主脚本.该捆绑软件将包含完整的递归目录和您的主脚本.

main.sh is the main script. The bundle will contains the full recursive dirs and your main script.

运行捆绑包:

chmod +x bundle.run
./bundle.run

这篇关于存档重新生成Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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