반응형

셸과 각종 명령어들


셸은 리눅스/유닉스 셸은 텍스트 기반에서 사용자가 원하는 작업을 실행하고 그 명령을 운영체제를 통하여 수행하고 다시 사용자에게 결과를 출력하여 보여준다. bash는 그중 가장 많이 사용하는 셸 중에 하나이다.

bash 셸로 전환

#!/bin/bash


실행한 뒤 현재 디렉터리의 파일들이 잘 변경되었는지 확인하려면 ls -al 명령을 사용해야 한다. 이 명령을 스크립트에 포함하면 한 번 실행에 파일 목록까지 확인할 수 있다.

#!/bin/bash
echo test

를 입력하면 화면에 test 가 출력됨


ls -al 명령어 모습





4 Bash If Statement Examples(If then fi, If then else fi, If elif else fi, Nested if )





if 문 예제


Bash conditional statements perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. These statements are used to execute different parts of your shell program depending on whether certain conditions are true. The ability to branch makes shell scripts powerful.

In Bash, we have the following conditional statements:

  1. if..then..fi statement (Simple If)
  2. if..then..else..fi statement (If-Else)
  3. if..elif..else..fi statement (Else If ladder)
  4. if..then..else..if..then..fi..fi..(Nested if)

These are similar to the awk if statements we discussed earlier.

1. Bash If..then..fi statement

if [ conditional expression ]
then
	statement1
	statement2
	.
fi

This if statement is also called as simple if statement. If the given conditional expression is true, it enters and executes the statements enclosed between the keywords “then” and “fi”. If the given expression returns zero, then consequent statement list is executed.

if then fi example:

#!/bin/bash
count=100
if [ $count -eq 100 ]
then
  echo "Count is 100"
fi

2. Bash If..then..else..fi statement

If [ conditional expression ]
then
	statement1
	statement2
	.
else
	statement3
	statement4
	.
fi

If the conditional expression is true, it executes the statement1 and 2. If the conditional expression returns zero, it jumps to else part, and executes the statement3 and 4. After the execution of if/else part, execution resume with the consequent statements.

if then else fi example:

#!/bin/bash
count=99
if [ $count -eq 100 ]
then
  echo "Count is 100"
else
  echo "Count is not 100"
fi

Note: This article is part of the ongoing Bash Tutorial series.

3. Bash If..elif..else..fi

If [ conditional expression1 ]
then
	statement1
	statement2
	.
elif [ conditional expression2 ]
then
	statement3
	statement4
	.
.
.
else
	statement5
fi

You can use this if .. elif.. if , if you want to select one of many blocks of code to execute. It checks expression 1, if it is true executes statement 1,2. If expression1 is false, it checks expression2, and if all the expression is false, then it enters into else block and executes the statements in the else block.

if then elif then else fi example:

#!/bin/bash
count=99
if [ $count -eq 100 ]
then
  echo "Count is 100"
elif [ $count -gt 100 ]
then
  echo "Count is greater than 100"
else
  echo "Count is less than 100"
fi

4. Bash If..then..else..if..then..fi..fi..

If [ conditional expression1 ]
then
	statement1
	statement2
	.
else
	if [ conditional expression2 ]
	then
		statement3
		.
	fi
fi

If statement and else statement could be nested in bash. The keyword “fi” indicates the end of the inner if statement and all if statement should end with the keyword “fi”.

The “if then elif then else fi” example mentioned in above can be converted to the nested if as shown below.

#!/bin/bash
count=99
if [ $count -eq 100 ]
then
  echo "Count is 100"
else
  if [ $count -gt 100 ]
  then
    echo "Count is greater than 100"
  else
  echo "Count is less than 100"
  fi
fi

In our next article, we’ll discuss about how to use Bash conditional expressionswith practical examples.


ref : https://terms.naver.com/entry.nhn?docId=4125984&cid=59321&categoryId=59321

ref : https://www.thegeekstuff.com/2010/06/bash-if-statement-examples/

반응형

+ Recent posts