안녕하세요 bashshell 프로그래밍 질문드립니다.

hchan의 이미지

질문1

if [ "abc" = 1 ]
then
echo success!
fi

if문의 기본적으로 문법은 아래와 같은데요. 여기서 abc에 대한 비교조건을 여러개 둘 수 있는 지요? else if 제외(하나의 if문으로)

C언어의 경우 abc=1 | def=2 | ghi=3 이런 식으로 구분자를 넣어서 사용하는데요. bash에서는 어떻게 사용하는지 궁금합니다.

질문2
-------------------------

abc=1
./code2

------------------------------

echo $abc

-----------------------------

code1에서 ./code2 실행할 경우 code2로 변수가 공유되나요? 변수가 공유되지 않으면 방법이 어떻게 되는지 알 수 있을까요?

업무상 자동화가 필요해서 질문드립니다. 선배님들의 조언 부탁드릴께요. bash 문법이 익숙치 않아서 궁금한게 많네요~!

pwk100의 이미지

c언어에서 if랑 비슷하게 and, or 연산자 적용 가능한걸로 알고 있어요.

abc=1
def=1
if [ $abc = 1 ] && [ $def = 1 ]
then
echo success!!
fi

이런식으로 하면 abc 랑 def 가 둘 다 1 이니까 success!! 가 나오는 거죠.
두번째 질문은....죄송합니다 저도 뉴비...짧아요..ㅠㅠㅠ

hchan의 이미지

감사해요~

Prentice의 이미지

export abc=1

hchan의 이미지

전역 설정이 있었네요!!

마잇의 이미지

sh 기준 문법으로 하면 이렇게 쓰셔야 할 겁니다.

if [ $abc = "1" -o $abc = "2" ]

            These primaries can be combined with the following operators:
 
            ! expression  True if expression is false.
 
            expression1 -a expression2
                          True if both expression1 and expression2 are true.
 
            expression1 -o expression2
                          True if either expression1 or expression2 are true.

bash 문법으로는,

if [[ $abc == "1" || $abc == "2" ]]

이런 식으로도 됩니다.

http://www.tldp.org/LDP/abs/html/testconstructs.html

솔직히 좀 들여다 보면 문법이 좀 더럽습니다.

[...] <- 이 문법은 정확히 얘기하자면 명령어 실행 입니다. /usr/bin/[
test 명령어와 같습니다. man sh 하셔서 'test expression' 검색 하시면 설명이 나옵니다.
[...] 안의 내용이 좀 복잡해지면 사용하기가 좀 번거롭습니다.

bash 사용에 문제가 없는 경우는 [[ .... ]] 이 문법을 사용하시는 게 낫습니다.


--
마잇

hchan의 이미지

문법 배우고 갑니다~!!

댓글 달기

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