我在线发现这个脚本我正在尝试编辑,但是当我测试它时,我发现它会为我用空格的所有文件吐出一堆错误。这是我在终端窗口上看到的那种错误日志:
Skipping 1-03 as ./mp3/basename "$input_file" .wav.mp3 exists.
Skipping The as ./mp3/basename "$input_file" .wav.mp3 exists.
Skipping power, as ./mp3/basename "$input_file" .wav.mp3 exists.
这是剧本:
#!/bin/bash
# Title: wav_to_mp3.sh
# Purpose: Converts all WAV files present in the folder to MP3
# Author: Karthic Raghupathi, IVR Technology Group LLC
# Last Revised: 2014.01.28
# references
sox="/usr/local/bin/sox"
sox_options="-S"
# variables
source_folder="${1:-.}"
destination_folder="${source_folder}/mp3"
environment="${2:-DEVELOPMENT}"
# check to see if an environment flag was supplied
if [ $environment = "PRODUCTION" ] || [ $environment = "production" ]; then
sox="/usr/bin/sox"
environment="PRODUCTION"
fi
# print all params so user can see
clear
echo "Script operating using the following settings and parameters....."
echo ""
echo "which SoX: ${sox}"
echo "SoX options: ${sox_options}"
echo "Environment: ${environment}"
echo "Source: ${source_folder}"
echo "Destination: ${destination_folder}"
echo ""
read -e -p "Do you wish to proceed? (y/n) : " confirm
if [ $confirm = "N" ] || [ $confirm = "n" ]; then
exit
fi
# create destination if it does not exist
if [ ! -d "${destination_folder}" ]; then
mkdir -p "${destination_folder}"
fi
# loop through all files in folder and convert them to
for input_file in $(ls -1 $1 | grep .wav)
do
name_part=`basename "$input_file" .wav`
output_file="$name_part.mp3"
# create mp3 if file does not exist
if [ ! -f "$destination_folder/$output_file" ]; then
$sox $sox_options "${source_folder}/$input_file" "$destination_folder/$output_file"
else
echo "Skipping ${input_file} as $destination_folder/$output_file exists."
fi
done
我知道我应该让它逃脱太空人物,但我无法弄清楚如何。我试着在这里和那里更改一些引用,但我只是打破它。
顺便说一句,如果有人愿意链接一个很好的教程来学习如何在Mac OS(或Unix)上制作bash脚本,那将非常感激。我已经知道了一些网络编程,所以我不是一个完整的n00b,但是,我仍然无法创建非常简单的脚本,我想独立学习,而不必经常互联网寻求帮助:)< / p>答案 0 :(得分:1)
这是错误的:
for input_file in $(ls -1 $1 | grep .wav)
请参阅here原因。此外,在$ 1内,尝试这样看看带有空格的文件名会带来麻烦:
for i in $(ls -1 | grep wav); do echo $i; done
请改为尝试:
for input_file in $1/*.wav
答案 1 :(得分:-3)
您可以通过在空格前插入反斜杠字符来转义空格。
变化:
This\ file\ name
要:
mysqladmin variables | grep socket
编写一个函数来为您完成此操作可能是一个想法,遍历字符串中的每个字符并在任何空格之前添加\ caracter。这样您就不必担心预先格式化文件名并转义每个单独的空间 - 只需通过该函数运行文件名并捕获结果。