如何创建和运行 bash 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7370239/
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-09-18 00:45:07  来源:igfitidea点击:

How to create and run a bash file?

bashterminal

提问by Devang

How to create a bash file?

如何创建一个 bash 文件?

I found following bash script.

我发现了以下 bash 脚本。

#!/bin/bash
for f in *.wav; do
  echo "Processing $f file..."
  afconvert -f caff -d LEI16@44100 -c 1 "$f" "${f/wav/caf}"
done

I have Macand Windows.

我有MacWindows

But I don't know where I have to put this and how I can run this script?

但我不知道我必须把它放在哪里以及如何运行这个脚本?

采纳答案by nfechner

As you have two different OS setup, I will split my answer in two parts.

由于您有两个不同的操作系统设置,我将把我的答案分成两部分。

First: Windows

第一:窗户

Windows does not have a Bash interpreter, nor the afconvertprogram the code above is trying tu run. Your best bet will be to use Cygwinto install a Unix console on your Windows. Also I don't know, where you could get afconvertfrom.

Windows 没有 Bash 解释器,也没有afconvert上面的代码试图运行的程序。最好的办法是使用Cygwin在 Windows 上安装 Unix 控制台。我也不知道,你可以afconvert从哪里得到。

OSX

操作系统

OSX does have a console and the afconvertsoftware (at least my OSX does). You can simply drop the file in a folder and give it a name ending in .sh. Then you should be able to run it.

OSX 确实有一个控制台和afconvert软件(至少我的 OSX 有)。您可以简单地将文件放在一个文件夹中并为其命名以.sh. 然后你应该能够运行它。

回答by sashang

Just save the text into a file called my_bash_scripta file, make it executable and run it from the command line like this:

只需将文本保存到一个名为my_bash_scriptfile 的文件中,使其可执行并从命令行运行它,如下所示:

chmod u+x my_bash_script
./my_bash_script

Judging by the file it will need to be in a directory containing *.wav files.

从文件来看,它需要位于包含 *.wav 文件的目录中。

回答by Youssef

Just paste the text into a plaint text file and then mark it as executable:

只需将文本粘贴到纯文本文件中,然后将其标记为可执行文件:

chmod +x yourScript

To run it:

运行它:

./yourScript

回答by Johu

Place the script anywhere you have your .wav files. When fallow instructions given here: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html#sect_02_01_03

将脚本放置在您拥有 .wav 文件的任何位置。当这里给出休耕指令时:http: //tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html#sect_02_01_03