bash 질문입니다.

jbssy의 이미지

만약에 공백이 있는 파일을 복사하기 위에서는
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
Prentice의 이미지

cp $filename $TARGET 대신에 cp "$filename" "$TARGET" 이렇게 해보세요. 변수 내용에 스페이스가 들어갈지도 모르는 경우 큰따옴표로 감싸주시면 편해집니다.

jbssy의 이미지

감사합니다. :D

LINUH DESKTOP - Never be alone again

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.