简体   繁体   English

Linux-下载/提取脚本

[英]Linux - Download/extract Script

I am looking to integrate a script with my control panel and am having issues as i am a novice scripter. 我希望将脚本与控制面板集成在一起,并且由于我是新手脚本编写者而遇到问题。 What I would like to do is with 1 script download a file and extract it. 我想用1个脚本下载文件并解压缩。

Example: 例:

wget http://example.com/example.tar.gz wget http://example.com/example.tar.gz

tar -xvzf example.tar.gz -C / tar -xvzf example.tar.gz -C /

Do I need the following: #!/bin/bash at the start? 我是否需要以下内容:#!/ bin / bash开头?

Any help would greatly help. 任何帮助都会有很大帮助。 Thank you. 谢谢。

Yupp, basically save this to myscript.sh Yupp,基本上将其保存到myscript.sh

#!/bin/bash
wget -nv http://example.com/example.tar.gz
tar -zxvf example.tar.gz

then to execute it 然后执行它

./myscript.sh

More info about this here: http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html 有关此的更多信息,请访问: http : //www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html

The #! #! at the beginning (called a shebang ) is used when your file is executed as a program. 开头(称为shebang )用于文件作为程序执行时。 It tells the program loader what program to run the file with (/bin/bash in this case), so it is run using the command /bin/bash script_name.sh . 它告诉程序加载器使用哪个程序运行文件(在本例中为/ bin / bash),因此使用命令/bin/bash script_name.sh运行该程序。

If the line is there, and the file has executable permissions, you can run it by double-clicking in the GUI or running path/to/script_name.sh in the shell. 如果该行在那里并且文件具有可执行权限,则可以通过在GUI中双击或在外壳程序中运行path/to/script_name.sh来运行它。
Whether the line is there or not, you can always run it using bash path/to/script_name.sh 无论该行是否存在,您都可以始终使用bash path/to/script_name.sh运行它

You could create a generic script to download and extract any .tar.gz file: 您可以创建一个通用脚本来下载.tar.gz压缩任何.tar.gz文件:

#!/bin/bash
wget -qO- $1 | tar zxv

then do ./myscript.sh http://example.com/example2.tar.gz 然后执行./myscript.sh http://example.com/example2.tar.gz

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

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