bash 질문입니다.
글쓴이: jbssy / 작성시간: 토, 2004/03/06 - 8:02오후
만약에 공백이 있는 파일을 복사하기 위에서는 cp File\ name.txt Another/ File/ name.txt
라는 식으로 하면 됩니다.
이것을 이용해서 쉘스크립트를 만든다고 하면요
find $SOURCE -type f -print >> $tmpfile sed 's/[ ]/\\ /g' $tmpfile > $file
이렇게 하면 $file 에는 파일 명의 띄어쓰기에는 백슬러쉬와 공백이 적절하게 들어갑니다. $file에 있는 세번 째 행의 파일을 다른 파일로 복사하기 위해
TARGET="file3rd.xxx" filename=`sed -n 3p $file` cp $filename $TARGET
라고 썼는데요... cp 에러가 뜸니다.
cp: copying multiple files, but last argument `XXX' is not a directory 더 많은 정보를 보려면 `cp --help' 하십시오.
그냥 명령어로 잘되는데 스크립트로 하면 꼭 이러네요.. :shock:
어찌해야할까요...??
밑의 스크립트는 제가 만들려는 스크립트 전체의 소스파일 입니다.
#!/bin/bash #fn_euckr2utf8 : iconv를 이용해 디렉토리 전체의 파일명을 euckr 인코딩에서 #+ utf8로바꾸어주는 스크립트이다. #사용법 : fn_euckr2utf8 SOURCE_DIRECTORY OUTPUT_DIRECTORY #만약 파라미터가 두개가 아니라면 에러메시지를 출력하고 종료한다. if [ $# -ne 2 ] then echo "Please check the usage of `basename $0` " exit 0 fi #두번째 파라미터로 주어진 출력 디렉토리를 만든다. if [ -d "$1" ] then mkdir -p $2 SOURCE=`basename $1` TARGET=`basename $2` else echo "$1 is a not directory, please check the usage of $0" exit 0 fi #만약 source 디렉토리에 하위 디렉토리가 존재 한다면 그 디렉토리와 동일한 #디렉토리를 출력되는 디렉토리 안에 똑같이 만들어준다. for subdir in `find $SOURCE -type d -print` do mkdir -p $TARGET/${subdir#$SOURCE} done #파일의 목록을 저장할 파일을 만들어준다. euctmpfile="__euc_kr_filelist_.txt" utftmpfile="__utf_filelist_.txt" #공백 대신에 백슬러쉬와 공백으로 변환된 파일 리스트가 들어갈 텍스트 파일 b_euctmpfile="___euc_kr_filelist_.txt" b_utftmpfile="___utf_filelist_.txt" touch $euctmpfile touch $utftmpfile touch $b_euctmpfile touch $b_utftmpfile find $SOURCE -type f -print >> $euctmpfile iconv -f EUC-KR -t UTF-8 $euctmpfile -o $utftmpfile sed 's/[ ]/\\ /g' $euctmpfile > $b_euctmpfile sed 's/[ ]/\\ /g' $utftmpfile > $b_utftmpfile MAX_LINE_NUM=`cat $utftmpfile |wc -l` MAX_LINE_NUM=${MAX_LINE_NUM#[:space:]} echo $MAX_LINE_NUM i=0 while [ "$i" -le "$MAX_LINE_NUM" ] do i=`expr $i + 1` iprint=`echo $i`p b_euc_name_file=`sed -n $iprint $b_euctmpfile` b_utf_name_file=`sed -n $iprint $b_utftmpfile` cp $b_euc_name_file $TARGET${b_utf_name_file#$SOURCE} done rm -f $euctmpfile $utftmpfile $b_euctmpfile $b_utftmpfile exit 0
Forums:
[code:1]cp $filename $TARGET [/code:1] 대
cp $filename $TARGET
대신에cp "$filename" "$TARGET"
이렇게 해보세요. 변수 내용에 스페이스가 들어갈지도 모르는 경우 큰따옴표로 감싸주시면 편해집니다.jbssy
감사합니다. :D
LINUH DESKTOP - Never be alone again
댓글 달기