2014-10-31 56 views
0

我一直在尝试制作一个shell脚本,它将一个接一个地将文本文件分割成一个整个文件夹,并将每个分割块存入另一个指定文件夹。分割文件夹中的文本文件

这里是我到目前为止,我它可能是笨重(从来没有尝试过写.SH)之前知道:

#!/bin/bash 
#File Split Automation 

echo "Usage: split [Folder w/ Input] [Folder For Outputs] [Options] [PREFIX] 
    Options: -b [sizeMB]: Split by size 
    -l [No. of Lines]: Split by Lines 
    If No Output Folder is Defined Default is Set To: /Desktop/splitter-parts 
    If No Options Are Selected Default is Size=100MB" 

inputdirc=$1 
outputdirc=$2 
spltion=$3 
meastick=$4 
prefixture=$5 

if [ -d $1 ] 
then 
    echo "You Picked The Folder $1 To Split Files From" 
    ls $1 
else 
    exit 
fi 

if [ -d $2 ] 
then 
    echo "Please Confirm Folder Path For Output $outputdirc" 
else 
    cd /root/Desktop/ 
    mkdir -p splitter-parts 
fi 



read -t 10 -p "Press Enter Or Wait 5 Sec. To Continue" 


cd $2 

for swordfile in $(ls $1); 
do 
command -p split $3 $4 -a 3 -d $swordfile $5 

done 

任何你看到的问题呢?因为我没有得到我想要的输出,尽管它在split命令字符串中只有一个文件和一个文件夹时运行正常。

编辑::::

对不起,我很抱歉。只是稍微超前了一点。

这是我所看到的,当我运行它:

[email protected]:~/Desktop/Wordlists# ./splitter.sh '/root/Desktop/Wordlists'    ' /root/Desktop/Untitled Folder' s 100MB 
Usage: split [Folder w/ Input] [Folder For Outputs] [Options] [PREFIX] 
Options: -b [sizeMB]: Split by size 
-l [No. of Lines]: Split by Lines 
If No Output Folder is Defined Default is Set To: /Desktop/splitter-parts 
If No Options Are Selected Default is Size=100MB 
You Picked The Folder /root/Desktop/Wordlists To Split Files From 
10dig10milup2.txt      mixed.txt 
10dig10miluplow2.txt      movie-characters.txt 
10dig10miluplow3.txt      name1s.txt 
((------------------CUT------------) 
lower.lst       xae2.txt 
lower.txt       xaf2.txt 
mangled.lst      xag2.txt 
mangled.txt      xah6.txt 
misc-dictionary.txt 
./splitter.sh: line 24: [: /root/Desktop/Untitled: binary operator expected 
Press Enter Or Wait 5 Sec. To Continue 
./splitter.sh: line 37: cd: /root/Desktop/Untitled: No such file or directory 
split: extra operand `10dig10milup2.txt' 
Try `split --help' for more information. 
split: extra operand `10dig10miluplow2.txt' 
Try `split --help' for more information. 
split: extra operand `10dig10miluplow3.txt' 
Try `split --help' for more information. 
split: extra operand `10dig10miluplow4.txt' 
Try `split --help' for more information. 
...................MORE OF THE SAME....... 

至于什么,我应该看到,我还没有得到那么远呢,显然我错过了一些步骤。

+1

“我没有得到我想要的输出”不是一个明确的问题陈述。如果您能够清楚输入和理想的输出并准确解释您遇到的问题,它可以让我们更好地帮助您。 – 2014-10-31 09:56:09

+1

您是否认为我们可以猜到脚本输出是否正确以及输出错误?提供显示正确和错误输出的例子。 – Jdamian 2014-10-31 09:56:42

+1

为什么你定义不使用的shell变量? – Jdamian 2014-10-31 09:57:26

回答

2

快速改写了一些注意事项遵循:

#!/bin/bash 
#File Split Automation 

usage="Usage: split [Options] [Folder w/ Input] [Folder For Outputs] [PREFIX] 
    Options: -b [sizeMB]: Split by size 
    -l [No. of Lines]: Split by Lines 
    If No Output Folder is Defined Default is Set To: /Desktop/splitter-parts 
    If No Options Are Selected Default is Size=100MB" 

split_opt="-b 100MB" 
while getopts hb:l: opt; do 
    case $opt in 
     h) echo "$usage"; exit ;; 
     b) split_opt="-b $OPTARG" ;; 
     l) split_opt="-l $OPTARG" ;; 
    esac 
done 
shift $((OPTIND - 1)) 

if [[ $# -eq 0 ]]; then 
    echo "$usage" 
    exit 1 
fi 

inputdirc=$1 
if [[ -d $inputdirc ]]; then 
    ls $1 
else 
    echo "no such directory: $inputdirc" >&2 
    exit 1 
fi 

if [[ -n $2 ]]; then 
    outputdirc=$2 
else 
    outputdirc=/root/Desktop/splitter-parts 
fi 

prefixture=$3 

mkdir -p "$outputdirc" 
cd "$outputdirc" 

for swordfile in "$inputdirc"/*; do 
    command -p split $split_opt -a 3 -d "$swordfile" $prefixture 
done 

注:

  • 你一般要引用所有的变量。这是你错误的原因,因为名称中有一个带有空格和方括号的文件。
  • 我并没有引述一对情侣在split命令,因为我特别希望shell的值执行word splitting
  • 自选项,以及,可选的,使用getopts收集它们。
  • 您将位置参数存储在变量中,但您继续使用位置参数。挑一个或另一个。
+0

你还有一个没有引号的'ls $ 1'。另外,对于'split_opt',你应该肯定使用一个数组(问题被标记为Bash)并且适当引用。在'split'命令中引用'prefixture'如何?它受到路径名称扩展的影响!你可以使用'set -f'(但是......)。 – 2014-10-31 11:33:00

+0

另外,每个'cd'都应该跟着一个明确的检查,不管'cd'是否成功。 – 2014-10-31 11:34:50

+0

哇!感谢这么多人,说实话,我今晚在教科书中永远都不会那么做。我希望在高中学习计算机科学。 – Schmorrison 2014-10-31 11:43:03