自动运行的文件

时间:2013-04-03 13:27:43

标签: linux bash shell autorun

以同样的方式编写a file that autoextracts itself,我正在寻找一种在脚本中自动运行程序的方法(或者它需要的任何东西)。我想要程序部分的脚本,因为我只想要一个文件。这实际上是一个挑战:我有一个xz压缩程序,我希望能够在没有用户干预xz程序的情况下运行它(只需一个./theprogram)。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

做完什么后自动运行?登录?在~/.bashrc中调用它。在开机期间?编写一个适当的/etc/init.d/yourprog并将其链接到所需的运行级别。 Selfextract?使它成为shell存档(shar文件)。请参阅shar实用程序http://linux.die.net/man/1/shar

答案 1 :(得分:0)

很抱歉,但我只是在想...这样的事情不起作用? (我假设它是一个脚本......)

#!/bin/bash

cat << 'EOF' > yourfile
yourscript
EOF

chmod +x yourfile

./yourfile

但是,很难理解你要做什么......在我看来,“自动运行”非常类似于“从脚本中调用程序”..

答案 2 :(得分:0)

我为此编写了一个脚本。这应该有所帮助:

#!/bin/bash
set -e
payload=$(cat $0 | grep --binary-files=text -n ^PAYLOAD: | cut -d: -f1 )
filaname=`head $0 -n $payload | tail -n 1 | cut -d: -f2-`
tail -n +$(( $payload + 1 )) $0 > /tmp/$filaname
set +e
#Do whatever with the payload
exit 0
#Command to add payload:
#read x; ls $x && ( cp 'binary_script.sh' ${x}_binary_script.sh; echo $x >>  ${x}_binary_script.sh; cat $x >> ${x}_binary_script.sh  )
#Note: Strictly NO any character after "PAYLOAD:", not even newline...
PAYLOAD:

样品用法:
假设myNestedScript.sh包含以下数据:

#!/bin/bash
echo hello world

然后运行

x=myNestedScript.sh; ls $x && ( cp 'binary_script.sh' ${x}_binary_script.sh; echo $x >>  ${x}_binary_script.sh; cat $x >> ${x}_binary_script.sh  )

它将生成以下文件,您可以直接执行该文件。执行以下文件后,它会将myNestedScript.sh提取到/tmp&amp;运行该脚本。

#!/bin/bash
set -e
payload=$(cat $0 | grep --binary-files=text -n ^PAYLOAD: | cut -d: -f1 )
filaname=`head $0 -n $payload | tail -n 1 | cut -d: -f2-`
tail -n +$(( $payload + 1 )) $0 > /tmp/$filaname
set +e
chmod 755 /tmp/$filaname
/tmp/$filaname
exit 0
PAYLOAD:myNestedScript.sh
#!/bin/bash
echo hello world