bash 脚本无法处理文件名中的空格
bash script can't handle spaces in file names
我在网上找到了这个我正在尝试编辑的脚本,但在测试它时我发现它会为我拥有的所有文件吐出一堆错误 spaces .这是我在终端上得到的错误日志 window:
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 | 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
我知道我应该让它转义 space 个字符,但我不知道如何转义。我尝试在这里和那里更改一些引号,但我只是打破它。
顺便说一句,如果有人愿意 link 一个学习如何在 Mac OS(或 Unix)上制作 bash 脚本的好教程,那将不胜感激。我已经知道一些网络编程,所以我不是一个完整的 n00b,但是,我仍然无法创建非常简单的脚本,我想独立学习而不是不断地窃听互联网寻求帮助:)
您可以通过在 space 前插入反斜杠字符来转义 space。
变化:
This file name
收件人:
This\ file\ name
编写一个函数来为您执行此操作可能是个好主意,遍历字符串中的每个字符并在任何 space 之前添加一个 \ 字符。这样你就不需要担心预先格式化文件名和转义每个人 space - 只需 运行 通过函数的文件名并捕获结果。
这是错误的:
for input_file in $(ls -1 | grep .wav)
查看 here 原因。另外,在 $1 内,试试看带空格的文件名是否有问题:
for i in $(ls -1 | grep wav); do echo $i; done
试试这个:
for input_file in /*.wav
我在网上找到了这个我正在尝试编辑的脚本,但在测试它时我发现它会为我拥有的所有文件吐出一堆错误 spaces .这是我在终端上得到的错误日志 window:
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 | 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
我知道我应该让它转义 space 个字符,但我不知道如何转义。我尝试在这里和那里更改一些引号,但我只是打破它。
顺便说一句,如果有人愿意 link 一个学习如何在 Mac OS(或 Unix)上制作 bash 脚本的好教程,那将不胜感激。我已经知道一些网络编程,所以我不是一个完整的 n00b,但是,我仍然无法创建非常简单的脚本,我想独立学习而不是不断地窃听互联网寻求帮助:)
您可以通过在 space 前插入反斜杠字符来转义 space。
变化:
This file name
收件人:
This\ file\ name
编写一个函数来为您执行此操作可能是个好主意,遍历字符串中的每个字符并在任何 space 之前添加一个 \ 字符。这样你就不需要担心预先格式化文件名和转义每个人 space - 只需 运行 通过函数的文件名并捕获结果。
这是错误的:
for input_file in $(ls -1 | grep .wav)
查看 here 原因。另外,在 $1 内,试试看带空格的文件名是否有问题:
for i in $(ls -1 | grep wav); do echo $i; done
试试这个:
for input_file in /*.wav