[bash] $1 에 와일드카드 매칭 시키는 방법에 관해서 질문입니다

futurizer의 이미지

아래와 같이 shell.sh를 만듭니다.


#!/bin/sh

echo "$1"
ls "$1"

sh shell.sh /etc/ 라고 했을 경우
ls /etc/ 가 되어서 정상 출력 됩니다.

sh shell.sh /etc/*.conf 라고 하면
원하는 형태대로 동작하지 않는 군요.

이때 유저 즉 제가 sh shell.sh /etc/\*.conf 라고
\ 로 이스케이프 시켜주면 원하는 결과가 나오네요.

실제로 쉘을 사용할때 불편을 덜기 위해서 쉘 내부적으로 처리하고 싶은데
고수님들의 조언 부탁합니다.

:D

nohmad의 이미지

질문이 잘 이해가 안되는군요.
스크립트에서 * 문자를 * 문자 자체로 받기를 원하시는 겁니까?
아니면 와일드카드로 확장된 배열 전체를 받기를 원하시는 겁니까?

전자라면, 이스케이프 외에는 방법이 없을 것 같고,
후자라면, $@ 또는 $*와 같이 배열 전체를 받도록 해야 합니다.

futurizer의 이미지

아 질문의 명확성이 조금떨어지는듯 하네요.
죄송합니다.

shell.sh 에 첫번째 파라메타가 무엇이 올지 명확하지 않은 입장에서
와일드 카드 일 수도 있고 정해진 PATH 일 수도 있습니다.

첫번째 파라메타를 일상적으로
cp /etc /anywhere라고 입력하거나
cp /etc/*.conf /anywhere 라고 입력하는데

위에 작성한 쉘에서 와일드 카드를 사용할땐 그냥 쓰면 와일드 카드에
단하나의 파일만 돌아오는 현상이 생깁니다.

첫째 
sh tmp.sh /etc/*.conf
/etc/aep.conf
/etc/aep.conf

둘째
sh tmp.sh /etc/\*.conf
/etc/aep.conf /etc/aeplog.conf /etc/cdrecord.conf /etc/dovecot.conf /etc/esd.conf /etc/fam.conf /etc/gpm-root.conf 
/etc/grub.conf /etc/host.conf /etc/htdig.conf /etc/initlog.conf /etc/jwhois.conf /etc/krb.conf /etc/krb5.conf 
/etc/ld.so.conf /etc/ldap.conf /etc/lftp.conf /etc/libuser.conf /etc/logrotate.conf /etc/ltrace.conf /etc/modprobe.conf ....

이때 역슬래쉬를 넣지 않고 원하는 결과를 얻고 싶습니다.
즉 첫째 입력처럼 [일상적인] 입력을 하고 둘째 결과 처럼 와일드카드가
매치되길 바라는거죠.

이때 쉘에서 처리해줄수 있는 방법이 있나요?

항상 지금처럼, 내 색을 전하는, 내 따뜻함이길..
지란지교를 꿈꾸고... 전하고... 행하길..

Pure-Eternity's GuardianDevil-Kind
Paradox seeker who try to redeem myself.

The Futurizer.

pynoos의 이미지

* 가 만약 명령행에서 확장되지 않게 하려면

set -f

를 해주면됩니다.
원하시는 것은 * 를 그대로 tmp.sh로 넘겨야하는데, 현재 작업중인 shell이 set -f 안되어 있으면 확장되어 넘어가기 마련입니다.

따라서 tmp.sh 는 여러 파일을 받을 수 있도록 처리하는 것이 좋습니다.

futurizer의 이미지

네 원하는 결과를 얻었군요.
감사합니다. 몇가지 시험도 해보고 정리해봐야겠네요 ^_^

항상 지금처럼, 내 색을 전하는, 내 따뜻함이길..
지란지교를 꿈꾸고... 전하고... 행하길..

Pure-Eternity's GuardianDevil-Kind
Paradox seeker who try to redeem myself.

The Futurizer.

댓글 달기

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