【问题标题】:Splitting text files in folder拆分文件夹中的文本文件
【发布时间】:2014-10-31 09:45:57
【问题描述】:

我一直在尝试制作一个 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

你发现有什么问题吗?因为我没有得到我想要的输出,尽管当我在拆分命令字符串中有一个文件和一个文件夹时它运行良好。

编辑::::

对不起,我很抱歉。只是有点超前了。

这是我运行它时看到的:

root@kali:~/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.......

就我应该看到的而言,我还没有走那么远,显然我错过了一些步骤。

【问题讨论】:

  • “我没有得到我想要的输出”不是一个明确的问题陈述。如果您清楚地输入和期望输出并准确解释您遇到的问题,这将使我们能够更好地帮助您。
  • 你认为我们能猜出什么脚本输出是对的,什么输出是错的吗?提供显示正确和错误输出的示例。
  • 为什么要定义不用的shell变量?
  • Jdamian- 你指的是 $1 $2 等吗?
  • 我认为 Jdamian 指的是第一个分配,例如inputdirc=$1,然后您继续在代码中使用$1,而不是$inputdirc。祝你好运。

标签: linux bash split


【解决方案1】:

快速重写并附上一些注释:

#!/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 收集它们。
  • 您将位置参数存储在变量中,但您继续使用位置参数。任选其一。

【讨论】:

  • 你还有一个未引用的ls $1。此外,对于split_opt,您绝对应该使用数组(问题标记为 Bash)并适当引用。在split 命令中引用prefixture 怎么样?它受路径名扩展的影响!你可以使用set -f(但是嗯……)。
  • 另外,每个cd 之后都应该明确检查cd 是否成功。
  • 哇!非常感谢,老实说,今晚的教科书我永远不会走那么远。我希望我在高中学习计算机科学。
  • @gniourf_gniourf,一些优点。 prefixture 未加引号,因为如果未设置,我不想将空字符串传递给 split,我根本不想传递那个 arg。如果用户希望分割文件以*? 为前缀,他们可以通过其他方式在自己的脚下开枪。
  • 谢谢大家..我最终在命令拆分行中使用了“$swordfile”“$swordfile”,它完全符合我的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 2012-06-16
  • 1970-01-01
  • 2018-12-29
  • 2014-08-14
  • 2015-04-20
相关资源
最近更新 更多