리눅스 / 명령어 / 연산자 - ;, &&, ||

리눅스 셸에서 ;, &&, || 는 명령어 실행 방식을 제어하는 연산자입니다. 각 기호의 의미와 사용법은 다음과 같습니다.

;

명령어를 순차적으로 실행합니다. 앞의 명령이 성공하든 실패하든 다음 명령어를 실행합니다.

예를 들어 temp 디렉토리가 있는 경우 mkdir temp 명령은 실패합니다. 그래도 ll 명령을 실행합니다.

# mkdir temp ; ll
mkdir: cannot create directory ‘temp’: File exists
total 40
drwx------  6 root root 4096 May 28 00:14 ./
drwxr-xr-x 23 root root 4096 May 22  2024 ../
-rw-------  1 root root 1531 May 28 00:14 .bash_history
-rw-r--r--  1 root root 3106 Apr 22  2024 .bashrc
drwx------  2 root root 4096 May 22  2024 .cache/
-rw-------  1 root root   20 May 28 00:14 .lesshst
drwxr-xr-x  3 root root 4096 May 22  2024 .local/
-rw-r--r--  1 root root  161 Apr 22  2024 .profile
drwx------  2 root root 4096 May 22  2024 .ssh/
drwxr-xr-x  3 root root 4096 May 27 23:27 temp/

&&

앞의 명령어를 실행하고, 성공하면 뒤의 명령어를 실행합니다.

예를 들어 temp 디렉토리가 있는 경우 mkdir temp 명령은 실패합니다. 그래서 ll 명령은 실행하지 않습니다.

# mkdir temp && ll
mkdir: cannot create directory ‘temp’: File exists

||

앞의 명령어를 실행하고, 실패하면 뒤의 명령어를 실행합니다.

예를 들어 temp 디렉토리가 있는 경우 mkdir temp 명령은 실패합니다. 그래서 ll 명령을 실행합니다.

# mkdir temp || ll
mkdir: cannot create directory ‘temp’: File exists
total 40
drwx------  6 root root 4096 May 28 00:14 ./
drwxr-xr-x 23 root root 4096 May 22  2024 ../
-rw-------  1 root root 1531 May 28 00:14 .bash_history
-rw-r--r--  1 root root 3106 Apr 22  2024 .bashrc
drwx------  2 root root 4096 May 22  2024 .cache/
-rw-------  1 root root   20 May 28 00:14 .lesshst
drwxr-xr-x  3 root root 4096 May 22  2024 .local/
-rw-r--r--  1 root root  161 Apr 22  2024 .profile
drwx------  2 root root 4096 May 22  2024 .ssh/
drwxr-xr-x  3 root root 4096 May 27 23:27 temp/

 

같은 카테고리의 다른 글

Ubuntu / 설치 USB 만드는 방법

ODD가 없는 컴퓨터에 OS를 설치하는 방법은 두가지입니다. 첫번째는 휴대용 ODD를 이용하는 것이고, 두번째는 USB에 설치 파일을 넣어서 이용하는 것입니다. 두번째 방법이 간편하기는 하지만 설치용 USB를 만드는 게 좀 번거롭습니다. 하지만, 우분투(Ubuntu) 설치용 USB는 몇 번의 클릭으로 쉽게 만들 수 있습니다. 우분투 다운로드 원하는 우분투 버전을 다운로드 받습니다. Universal USB Installer 다운로드 다음의 링크에서 Universal USB Installer를 다운로드 ...

리눅스 / 명령어 / systemctl

systemctl은 서비스를 관리하는 명령어이다. 서비스 상태 확인 systemctl status service_name.service 서비스 시작 systemctl start service_name.service 서비스 재시작 systemctl restart service_name.service 서비스 중지 systemctl stop service_name.service 부팅 시 서비스 자동 시작 systemctl enable service_name.service 부팅 시 서비스 자동 시작 해제 systemctl disable service_name.service 서비스 목록 보기 systemctl list-units --type=service

Ubuntu 20.04 Server / hostname 확인하는 방법, 변경하는 방법

Ubuntu 20.04 Server / hostname 확인하는 방법, 변경하는 방법

hostname 확인하는 방법 방법 1 - 명령어로 확인하기 다음과 같이 명령하면 hostname을 출력한다. hostname 방법 2 - 설정 파일 열어서 확인하기 /etc/hostname 파일에 hostname이 있다. cat /etc/hostname hostname 변경하는 방법 방법 1 - 명령어로 변경하기 다음과 같이 명령하면 hostname이 abc로 바뀐다. hostnamectl set-hostname abc 방법 2 - 설정 파일 열어서 변경하기 텍스트 에디터로 /etc/hostname 파일을 열어서 내용을 abc로 바꾸면 hostname이 abc로 바뀐다. 재부팅 재부팅을 하면 변경사항이 ...

CentOS 7 / Samba / 설치하고 공유 폴더 만드는 방법

CentOS 7 / Samba / 설치하고 공유 폴더 만드는 방법

Samba 설치 Samba 패키지 이름은 samba입니다. yum install samba 방화벽 설정 Samba가 사용하는 포트를 열어줍니다. firewall-cmd --permanent --zone=public --add-service=samba 방화벽을 다시 로드합니다. firewall-cmd --reload 참고 : CentOS 7 / firewall-cmd 명령어로 방화벽 관리하기 SELinux SELinux를 적절히 설정하거나 끕니다. 참고 : CentOS 7 / SELinux 끄는 방법 Samba 사용자 추가 사용자가 Samba를 사용할 수 있도록 해줍니다. 만약 사용자계정이 user1이라면 다음과 같이 합니다. smbpasswd -a user1 Samba 설정 Samba 설정 ...

Ubuntu 20.04 Server / GUI 설치하는 방법

Ubuntu 20.04 Server / GUI 설치하는 방법

Ubuntu Server는 CLI(Command Line Interface) 환경으로 설치된다. 그런데 어떤 작업은 GUI(Graphical Uer Interface) 환경에서 하는 것이 편하다. 만약 GUI 환경이 필요하다면 설치하여 사용할 수 있다. GUI 패키지에는 kubuntu-desktop lubuntu-desktop ubuntu-desktop ubuntu-desktop-minimal xubuntu-desktop 등이 있다. 주로 ubuntu-desktop 또는 ubuntu-desktop-minimal을 사용하는 듯 하다. 예를 들어 ubuntu-desktop-minimal을 설치한다면, 다음과 같이 명령하면 된다. # apt install ubuntu-desktop-minimal Minimal임에도 불구하고 꽤 많은 저장소 공간이 필요하다. After ...

리눅스 / 파일 또는 디렉토리 이름 바꾸는 방법

파일 이름 바꾸기 mv는 파일을 이동시키는 명령어이다. 이 명령어를 이용해서 파일 이름을 바꿀 수 있다. 예를 들어 다음은 abc.txt를 xyz.txt로 바꾼다. # mv abc.txt xyz.txt 디렉토리 이름 바꾸기 디렉토리 이름을 바꾸는 것도 파일 이름 변경과 같다. 다음은 abc 디렉토리의 이름을 xyz로 바꾼다. # mv abc xyz

리눅스 / 커널 / 버전 확인하는 방법

리눅스 커널 버전을 확인하는 방법은 여러 가지가 있습니다. 그 중 자주 사용하는 방법 두 가지를 소개합니다. uname uname 명령어는 시스템 정보를 출력하는 데 사용됩니다. -r 옵션을 사용하여 커널 버전을 확인할 수 있습니다. # uname -r 6.8.0-35-generic hostnamectl hostnamectl 명령어는 시스템의 호스트 이름과 관련된 정보를 관리하고 표시합니다. 여기에는 커널 버전 정보도 포함됩니다. # hostnamectl Static hostname: ubuntu-24-01 ...

리눅스 / 하드디스크 파디션 만들기, 포맷하기, 마운트하기

리눅스에서 하드디스크 파티션 만들고, 포맷 하고, 마운트 하는 방법을 요약한다. 장착된 하드디스크 확인하기 ls 명령어로 컴퓨터에 장착된 하드디스크 정보를 출력할 수 있다. /dev/sda, /dev/sdb 등이 하드디스크, /dev/sda1 등이 파티션이다. # ls -l /dev/sd* brw-rw----. 1 root disk 8, 0 Oct 16 13:12 /dev/sda brw-rw----. 1 root disk 8, 1 Oct 16 13:12 /dev/sda1 brw-rw----. 1 ...

리눅스 / 명령어 / which, whereis, locate / 명령어 위치 찾기

명령어의 위치를 찾을 때 사용할 수 있는 명령어에는 which, whereis, locate가 있다. 명령어의 위치만 찾을 때는 which를 사용하고, 관련된 파일들의 위치까지 찾을 때는 whereis나 locate를 사용한다. which 명령어로 find 명령어를 찾는다. # which find /usr/bin/find whereis 명령어로 find 명령어를 찾는다. # whereis find find: /usr/bin/find /usr/share/man/man1/find.1.gz locate 명령어로 find 명령어를 찾는다. # locate find /usr/bin/find /usr/bin/find2perl /usr/bin/findmnt /usr/bin/nl-link-ifindex2name /usr/bin/nl-link-name2ifindex /usr/bin/oldfind /usr/lib64/python2.7/modulefinder.py /usr/lib64/python2.7/modulefinder.pyc /usr/lib64/python2.7/modulefinder.pyo /usr/sbin/btrfs-find-root /usr/sbin/findfs /usr/share/bash-completion/completions/findmnt /usr/share/doc/findutils-4.5.11 /usr/share/doc/findutils-4.5.11/AUTHORS /usr/share/doc/findutils-4.5.11/COPYING /usr/share/doc/findutils-4.5.11/ChangeLog /usr/share/doc/findutils-4.5.11/NEWS /usr/share/doc/findutils-4.5.11/README /usr/share/doc/findutils-4.5.11/THANKS /usr/share/doc/findutils-4.5.11/TODO /usr/share/doc/wpa_supplicant-2.6/examples/p2p/p2p_find.py /usr/share/doc/wpa_supplicant-2.6/examples/p2p/p2p_stop_find.py /usr/share/info/find-maint.info.gz /usr/share/info/find.info.gz /usr/share/locale/be/LC_MESSAGES/findutils.mo /usr/share/locale/bg/LC_MESSAGES/findutils.mo /usr/share/locale/ca/LC_MESSAGES/findutils.mo /usr/share/locale/cs/LC_MESSAGES/findutils.mo /usr/share/locale/da/LC_MESSAGES/findutils.mo /usr/share/locale/de/LC_MESSAGES/findutils.mo /usr/share/locale/el/LC_MESSAGES/findutils.mo /usr/share/locale/eo/LC_MESSAGES/findutils.mo /usr/share/locale/es/LC_MESSAGES/findutils.mo /usr/share/locale/et/LC_MESSAGES/findutils.mo /usr/share/locale/fi/LC_MESSAGES/findutils.mo /usr/share/locale/fr/LC_MESSAGES/findutils.mo /usr/share/locale/ga/LC_MESSAGES/findutils.mo /usr/share/locale/gl/LC_MESSAGES/findutils.mo /usr/share/locale/hr/LC_MESSAGES/findutils.mo /usr/share/locale/hu/LC_MESSAGES/findutils.mo /usr/share/locale/id/LC_MESSAGES/findutils.mo /usr/share/locale/it/LC_MESSAGES/findutils.mo /usr/share/locale/ja/LC_MESSAGES/findutils.mo /usr/share/locale/ko/LC_MESSAGES/findutils.mo /usr/share/locale/lg/LC_MESSAGES/findutils.mo /usr/share/locale/lt/LC_MESSAGES/findutils.mo /usr/share/locale/ms/LC_MESSAGES/findutils.mo /usr/share/locale/nl/LC_MESSAGES/findutils.mo /usr/share/locale/pl/LC_MESSAGES/findutils.mo /usr/share/locale/pt/LC_MESSAGES/findutils.mo /usr/share/locale/pt_BR/LC_MESSAGES/findutils.mo /usr/share/locale/ro/LC_MESSAGES/findutils.mo /usr/share/locale/ru/LC_MESSAGES/findutils.mo /usr/share/locale/rw/LC_MESSAGES/findutils.mo /usr/share/locale/sk/LC_MESSAGES/findutils.mo /usr/share/locale/sl/LC_MESSAGES/findutils.mo /usr/share/locale/sr/LC_MESSAGES/findutils.mo /usr/share/locale/sv/LC_MESSAGES/findutils.mo /usr/share/locale/tr/LC_MESSAGES/findutils.mo /usr/share/locale/uk/LC_MESSAGES/findutils.mo /usr/share/locale/vi/LC_MESSAGES/findutils.mo /usr/share/locale/zh_CN/LC_MESSAGES/findutils.mo /usr/share/locale/zh_TW/LC_MESSAGES/findutils.mo /usr/share/man/man1/find.1.gz /usr/share/man/man1/find2perl.1.gz /usr/share/man/man1/oldfind.1.gz /usr/share/man/man8/btrfs-find-root.8.gz /usr/share/man/man8/findfs.8.gz /usr/share/man/man8/findmnt.8.gz locate 명령어는 -n 옵션으로 출력 ...

리눅스 / 명령어 / cp - 복사하는 명령어, mv - 이동하는 명령어

cp는 copy의 약자로 복사하는 명령어, mv는 move의 약자로 이동하는 명령어이다. 복사하기 abc.txt 파일을 def.txt로 이름을 바꾸어 복사한다. cp abc.txt def.txt xyz라는 디렉토리가 없다면 abc.txt 파일을 xyz 파일로 복사한다. xyz라는 디렉토리가 있다면 xyz 디렉토리 안에 abc.txt 파일을 복사한다. cp abc.txt xyz abc.txt 파일을 xyz 디렉토리 안에 def.txt라는 이름으로 복사한다. cp abc.txt xyz/def.txt abc가 디렉토리이고 xyz라는 디렉토리가 없다면, abc 디렉토리를 xyz로 ...