2014-10-31 3 views
0

전체 폴더를 통해 텍스트 파일을 차례로 분할하고 모든 분할 된 청크를 다른 지정된 폴더에 보관하는 셸 스크립트를 만들려고했습니다. 당신이 볼폴더에 텍스트 파일 나누기

#!/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 

일이 잘못 갈 : 여기

나는 그 아마 투박한 (.sh로를 작성 해본 적이 전에) 알고, 내가 지금까지 가지고 무엇인가? 필자는 원하는 출력을 얻지 못하기 때문에 분할 명령 문자열에 파일과 폴더가있을 때 제대로 작동했지만

EDIT :::

죄송합니다. 사과드립니다. 그냥 나 자신보다 조금 앞서.

이 내가 그것을 실행할 때 내가보고있는 무슨이다 :

지금까지 내가 볼 수 있겠 것과
[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

"나는 원하는 출력을 얻지 못했습니다."는 명확한 문제는 아닙니다. 입력과 원하는 결과물을 명확하게 제시하고 문제의 원인을 정확하게 설명하면 더 잘 도와 드릴 수 있습니다. –

+1

스크립트 출력이 맞고 출력이 잘못되었다고 생각할 수 있으십니까? 옳고 그름을 보여주는 예제를 제공하십시오. – Jdamian

+1

왜 사용되지 않는 쉘 변수를 정의합니까? – Jdamian

답변

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 

참고 :

  • 당신이 일반적으로 모든 당신의 변수를 인용하고자합니다. 이것은 이름에 공백과 대괄호가있는 파일이 있었기 때문에 오류의 원인입니다. 내가 특별히 옵션이기 때문에
  • , 잘, 선택 사항을 수집하는 getopts을 사용하는 쉘이 값에 word splitting을 수행 할 때문에
  • 은 내가 split 명령 몇 가지를 인용하지 않았다.
  • 위치 매개 변수를 변수에 저장하지만 위치 매개 변수는 계속 사용합니다. 둘 중 하나를 선택하십시오.
+0

여전히 'ls $ 1'은 따옴표가 붙지 않았습니다.또한,'split_opt'에 대해서, 배열 (질문은 Bash라고 표기 됨)을 사용하고 적절히 인용해야합니다. 'split' 명령에서'prefixture'를 인용하는 것은 어떻습니까? 경로명 확장이 필요합니다! 당신은'set -f' (그러나 meh ...)를 사용할 수 있습니다. –

+0

또한'cd'가 성공했는지 여부에 관계없이'cd'마다 명시 적 검사가 이어져야합니다. –

+0

와우 !!! 고마워요. 오늘 밤 교과서에서 그런 말을 결코 얻지 못했을 것입니다. 고등학교에서 컴퓨터 과학을 듣기를 바랍니다. – Schmorrison