我需要能够在CentOS和Windows之间来回切换我在两个系统上开发的C ++项目。我希望能够将给定类型的所有文件(例如* .cpp,* .h或* .txt)从Unix转换为Dos。
我有一个转换单个文件的bash脚本,但我希望能够只键入文件类型的扩展名或* .FILETYPE来转换给定目录中的所有文件类型。
这是工作脚本,我需要做些什么才能让它一次适用于多个文件。我尝试过使用for循环,但是我遇到了语法错误。
#! /bin/sh
# Convert a text based file from Unix Format to Dos Format
# The Output file should be the name of the input file with
# WF inserted before the file extention
#
# Examples
# convert Unix format abc.txt to abcWF.txt Windows Format
# convert Unix format abc.cpp to abcWF.cpp Windows Format
echo "In u2w FileToConvert = $1"
FileBase=$(basename $1)
FileExtension="${FileBase##*.}"
FileBase="${FileBase%.*}"
OutputSuffix="WF.$FileExtension"
OutputFile="$FileBase$OutputSuffix"
commandment="unix2dos -n $1 $OutputFile"
echo $commandment
unix2dos -n $1 $OutputFile
echo "diff -w $1 $OutputFile"
diff -w "$1" "$OutputFile"
exit
运行示例
$ bd2w TestResults.txt
In u2w FileToConvert = TestResults.txt
unix2dos -n TestResults.txt TestResultsWF.txt
unix2dos: converting file TestResults.txt to file TestResultsWF.txt in DOS format ...
diff -w TestResults.txt TestResultsWF.txt
答案 0 :(得分:2)
将脚本包裹在for file in "$@"
循环中,并将$1
的所有提及替换为$file
。
#!/bin/sh
for file in "$@"; do
echo "In u2w FileToConvert = $file"
FileBase=$(basename "$file")
FileExtension="${FileBase##*.}"
FileBase="${FileBase%.*}"
OutputSuffix="WF.$FileExtension"
OutputFile="$FileBase$OutputSuffix"
echo "unix2dos -n $file $OutputFile"
unix2dos -n "$file" "$OutputFile"
echo "diff -w $file $OutputFile"
diff -w "$file" "$OutputFile"
done