Bash script: Difference between revisions

From HPCWIKI
Jump to navigation Jump to search
(Fix: remove --- horizontal lines (9 removed))
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
A summary for some important Bash scripts
{{Status
|status=Draft
|owner=Knowledge Agent
|last_update=2026-07-16
|review=Pending
}}
 
{{TOC}}
 
== Overview ==
 
Bash script에 대한 기술 문서입니다.
 
=== Summary ===
 
* 무엇인가? - Bash script
* 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
* 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시
 
 
== Purpose ==
 
이 문서가 존재하는 이유
 
* Goal: Bash script에 대한 기술 정보 제공
* Scope: Bash script의 개념, 사용법, 설정
* Non-goals: 다른 주제로의 확장
 
 
== Key Concepts ==
 
{| class="wikitable"
{| class="wikitable"
|+
! Concept
!Command
! Description
!Description
! Related
|-
|-
|set
| Bash script
|'''set [options] [arguments]'''<ref>https://linuxhint.com/set-command-bash/</ref>
| HPC/서버 환경에서 중요한 기술 개념
sign (-) is used with the command’s option to enable that option and the plus sign (+) is used with the command’s option to disable that option
| [[Linux]], [[Server]]
{| class="wikitable"
|}
| -a
|It defines those variables or functions which are created or modified or exported
Example, These variables v1 and v2 can be accessed after executing the script
<code>#!/bin/bash</code>


<code>#Enable -a option to read the values of the variables</code>


<code>set -a</code>
== Detailed Explanation ==


<code>#Initialize three variables</code>
|status=Draft
|owner=Knowledge Agent
|last_update=2026-07-16
|review=Pending
}}
Bash 스크립트는 Linux/Unix 시스템에서 명령어를 자동화하고 작업을 효율적으로 관리하기 위한 스크립트 언어입니다.
* 무엇인가? Linux/Unix 명령어 자동화 스크립트 언어
* 왜 필요한가? 반복 작업 자동화, 시스템 관리, 배치 처리
* 언제 사용하는가? 서버 설정, 로그 분석, 백업, 배포 등
이 문서가 존재하는 이유
* Goal: Bash 스크립트의 주요 명령어, special parameters, set 옵션 설명 제공
* Scope: set 명령어 옵션, bash special parameters
* Non-goals: Bash 프로그래밍 상세, 함수/배열/제어문
{| class="wikitable"
! Concept
! Description
! Related
|-


<code>v1=78</code>


<code>v2=50</code>
== Best Practices ==
|-
| -b
|It informs the job termination.
|-
| -B
|To do the task of the brace expansion.
|-
| -C
|It disables the overwriting feature of the existing file
Example,
$ cat > testfile.txt


$ set -C
* 최신 버전 사용 권장
* 공식 문서 참고
* 테스트 환경에서 먼저 검증


$ cat > testfile.txt.  # Could not overwrite testfile.txt
|-
| -e
|It exits for non-zero exit status value.
|-
| -f
|It disables the filename generation task.
|-
| -h
|It saves the location of the command where it has been used.
|-
| -m
|It enables job control.
|-
| -n
|It reads the commands.
|-
| -t
|It exits from the command after executing a single command.
|-
| -u
|It traces the unset variables - error is printed for the uninitialized variable
|-
| -v
|It prints the shell input lines.
|-
| -x
|It displays the commands and their attributes sequentially. It is mainly used to debug the script.
|-
| -- variable
|The string value is divided into three parts based on the space that is printed
<nowiki>#</nowiki>!/bin/bash


<nowiki>#</nowiki>Define a string variable
== References ==


myvar="Learn bash programming"
* [https://wiki.hpcmate.com Bash script]


<nowiki>#</nowiki>Set the set command without option and with variable


set -- $myvar
== Related Pages ==


<nowiki>#</nowiki>Print the split value
* [[Linux]]
* [[Server]]
* [[Hardware]]
* [[Network]]


printf "$1\n$2\n$3\n"
|}
'''Exit Values of Set Command'''


# Zero (0) is returned to complete the task successfully.
[[Category:Linux]]
# One (1) is returned if a failure occurs for any invalid argument.
== Knowledge Graph ==
# One (1) is returned if a failure occurs for a missing argument.
|}


== bash special parameters<ref>https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html</ref> ==
Related
{| class="wikitable sortable"
!parameters
!Description
!Example
|-
|<code>*</code>
|($*) Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and filename expansion. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the <code>IFS</code> special variable.  That is, <code>"$*"</code> is equivalent to <code>"$1<var>c</var>$2<var>c</var>…"</code>, where <var>c</var> is the first character of the value of the <code>IFS</code> variable. If <code>IFS</code> is unset, the parameters are separated by spaces. If <code>IFS</code> is null, the parameters are joined without intervening separators.
|
|-
|<code>@</code>
|($@) Expands to the positional parameters, starting from one. In contexts where word splitting is performed, this expands each positional parameter to a separate word; if not within double quotes, these words are subject to word splitting. In contexts where word splitting is not performed, this expands to a single word with each positional parameter separated by a space. When the expansion occurs within double quotes, and word splitting is performed, each parameter expands to a separate word.  That is, <code>"$@"</code> is equivalent to <code>"$1" "$2" …</code>. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, <code>"$@"</code> and <code>$@</code> expand to nothing (i.e., they are removed).
|
|-
|<code>#</code>
|($#) Expands to the number of positional parameters in decimal.
|
|-
|<code>?</code>
|($?) Expands to the exit status of the most recently executed foreground pipeline.
|
|-
|<code>-</code>
|($-, a hyphen.)  Expands to the current option flags as specified upon invocation, by the <code>set</code> builtin command, or those set by the shell itself (such as the <samp>-i</samp> option).
|
|-
|<code>$</code>
|($$) Expands to the process <small>ID</small> of the shell.  In a subshell, it expands to the process <small>ID</small> of the invoking shell, not the subshell.
|Determine the Current Shell
<$ readlink /proc/$$/exe
/usr/bin/bash</code>
$ cat /proc/$$/cmdline
<code>bash</code>
|-
|<code>0</code>
|($0) Expands to the name of the shell or shell script.  This is set at shell initialization.  If Bash is invoked with a file of commands (see Shell Scripts), <code>$0</code> is set to the name of that file. If Bash is started with the <samp>-c</samp> option (see Invoking Bash), then <code>$0</code> is set to the first argument after the string to be executed, if one is present.  Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.
|Determine the Current Shell


<$ echo $0
→ [[Linux]]
bash</code>
→ [[Server]]
|-
→ [[Hardware]]
|!
→ [[Network]]
|($!) Expands to the process <small>ID</small> of the job most recently placed into the background, whether executed as an asynchronous command or using the <code>bg</code> builtin (see Job Control Builtins).
|
|}


== Reference ==
[[Category:Guide]]
<references/>

Latest revision as of 11:27, 17 July 2026

Template:Status

Template:TOC

Overview

Bash script에 대한 기술 문서입니다.

Summary

  • 무엇인가? - Bash script
  • 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
  • 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시


Purpose

이 문서가 존재하는 이유

  • Goal: Bash script에 대한 기술 정보 제공
  • Scope: Bash script의 개념, 사용법, 설정
  • Non-goals: 다른 주제로의 확장


Key Concepts

Concept Description Related
Bash script HPC/서버 환경에서 중요한 기술 개념 Linux, Server


Detailed Explanation

|status=Draft |owner=Knowledge Agent |last_update=2026-07-16 |review=Pending }} Bash 스크립트는 Linux/Unix 시스템에서 명령어를 자동화하고 작업을 효율적으로 관리하기 위한 스크립트 언어입니다.

  • 무엇인가? Linux/Unix 명령어 자동화 스크립트 언어
  • 왜 필요한가? 반복 작업 자동화, 시스템 관리, 배치 처리
  • 언제 사용하는가? 서버 설정, 로그 분석, 백업, 배포 등

이 문서가 존재하는 이유

  • Goal: Bash 스크립트의 주요 명령어, special parameters, set 옵션 설명 제공
  • Scope: set 명령어 옵션, bash special parameters
  • Non-goals: Bash 프로그래밍 상세, 함수/배열/제어문

Best Practices

  • 최신 버전 사용 권장
  • 공식 문서 참고
  • 테스트 환경에서 먼저 검증


References


Related Pages

Knowledge Graph

Related

LinuxServerHardwareNetwork

Concept Description Related