shell 스크립트 문제좀 물어봅니다.

익명 사용자의 이미지

파일 포맷이 같은 여러개의 파일을 각각 폴더로 이동시키려는데 이게 생각대로 잘 안되네요..

----------------------------
pwd : /home/a/

a 라는 폴더 안에 000-01 , 000-02 , 000-03 .... 000.50 <- 이렇개 50개의 파일이 있다 치고

01,02,03,04 ... 50 <- 이렇게 50개의 폴더가 있다 치면

xxx.sh 파일에 문법 작성후 각각 번호에 맞게 각 폴더로 이동시키려고 할려는데

000-01 파일 이름을 파싱해서 01을 따서 폴더 01과 같을 때 mv 하겠다 이렇게 했는데 잘안되네요.

1번쨰 질문 : 파일이름을 파싱하는데 01을 파싱해서 변수에 어떻게 넣어야하나요?

2 : 파싱해 만든 변수와 01,02...폴더 이름과 비교해서 같으면 mv 할려고하는데 어떤 방식으로 비교를 해야하나요?

힌트좀 주시면 ㄳ하겟습니다.

ymir의 이미지

이미 디렉토리가 만들어져 있는 상황이면 굳이 파싱할 것도 없이 디렉토리 이름에 맞는 파일을 가진 애들만 복사하면 될 것 같네요..

$ for dir in ?? ; do [ -d $dir ] && mv /home/a/???-$dir $dir ; done

파일명에 맞춰서 디렉토리를 만들어야 한다면 .. (bash 로..)

$ for file in /home/a/???-?? ; do mkdir -p ${file#*-} ; mv $file ${file#*-} ; done

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

익명 사용자의 이미지

위에 대로 하니깐 에러가 나네요..혹시 for dir in ?? <- 이 물음표 제가 알아서 적으라는 뜻인가요?

아니면 표현식이 있는건가요? 검색해보니 물음표에 대해서는 안나오는데..

에러 내용은

:systax error near unexpected token `do
:`for dir in ?? ; do

이렇게 나오는데 ??표가 먼지 확실히 몰라서

for dir in /home/a ; do
[ -d $dir ] && mv /home/a/000-$dir $dir ;
done

이렇게 작성해보았는데 그래도 같은 에러가 나네요

혹시 물음표 표현식이면 해석좀 해주시면 감사하겠습니다.
메인질문에 대한 힌트도 좀..

ymir의 이미지

? 는 * 과 마찬가지로 wildcard 입니다. 1 개의 any character 와 동일하죠.

for dir in ?? 는 현재 디렉토리에서 두글자로 이루어진 모든 파일 또는 디렉토리를 차례대로 $dir 이라는 변수로 가져오라는 뜻입니다.
현재 디렉토리에 두글자로 된 디렉토리가 있고, /home/a/ 에 000-?? 와 같이 디렉토리명과 같은 파일이 있으면 그 디렉토리로 이동시키라는 거죠.
문법 에러가 나올 자리가 아닐것 같은데 이상하네요. ;;

어떤 shell 을 쓰시고 계시는지..??

for dir in /home/a ; do
[ -d $dir ] && mv /home/a/000-$dir $dir ;
done

위 코드는 $dir 이라는 변수에 /home/a 라는 값만 들어가기 때문에 정확하게 동작하지 않습니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

eseo의 이미지

#!/bin/sh
 
for file in `ls a`
do
    target_dir=`echo $file | cut -b5-6`
    mv a/$file $target_dir
done

---
배려하는 마음을 갖자.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.