Linux 用于复制文件和文件夹并执行命令的简单 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12376551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:19:29  来源:igfitidea点击:

simple shell script to copy files and folders and also execute a command

linuxshellubuntu

提问by arun arun

I haven't written any Shell scripts before, but i have to write a simple shell script to do the following;

我之前没有写过任何 Shell 脚本,但我必须编写一个简单的 Shell 脚本来执行以下操作;

I will keep all the required files in a single folder and bundle it with this shell script as a tar file; so when the user runs the shell script, it needs to copy the respective files to the respective destinations.

我会将所有需要的文件保存在一个文件夹中,并将其作为 tar 文件与这个 shell 脚本捆绑在一起;所以当用户运行shell脚本时,需要将各自的文件复制到各自的目的地。

The execution of copy as follows:

复制的执行如下:

  1. copy the plugin.so file to /usrlib/mozilla/plugins/

  2. copy the .so library files to /usr/local/lib/

  3. copy some header files directories(folders) to /usr/local/include/

  1. 将 plugin.so 文件复制到 /usrlib/mozilla/plugins/

  2. 将 .so 库文件复制到 /usr/local/lib/

  3. 将一些头文件目录(文件夹)复制到 /usr/local/include/

and finally, need to do ldconfig.

最后,需要做ldconfig。

回答by Zagorax

Basically, you can add in a script any command you are able to type inside the terminal itself. Then, you have two options for executing it:

基本上,您可以在脚本中添加您能够在终端内部输入的任何命令。然后,您有两个执行它的选项:

  1. Execute it from the terminal with sh your_script.sh. You don't even need to give execute permission to it with this solution.
  2. Give it the execute permission and run it with ./your_script.sh.
  1. 从终端执行它sh your_script.sh。您甚至不需要使用此解决方案为其授予执行权限。
  2. 授予它执行权限并使用./your_script.sh.

For the second solution, you have to start the file with what is called a shebang. So your script will look like:

对于第二种解决方案,您必须以所谓的shebang. 所以你的脚本看起来像:

#!/bin/sh

cp path/to/source path/to/destination
cp path/to/source path/to/destination
cp path/to/source path/to/destination

ldconfig

echo "Done!"

Nothing else. Just write the commands one after the other. The first line is the so-called shebangand tells the shell which interpreter to use for the script.

没有其他的。只需一个接一个地编写命令。第一行是所谓的shebang,告诉 shell 脚本使用哪个解释器。

Note: the extension for shell scripts is usually .sh, but you can actually name your file however you prefer. The extension has no meaning at all.

注意:shell 脚本的扩展名通常是.sh,但您实际上可以根据自己的喜好命名文件。扩展名完全没有意义。

Good scripting!

好剧本!