리눅스 쉘 스크립트에서 여러 인수들을 전달해 사용한다.
기본 사용법
xargs [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]] [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]
[--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive] [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]
[--max-args=max-args] [--no-run-if-empty] [--version] [--help] [command [initial-arguments]]
Manual
NAME
xargs – construct argument list(s) and execute utility
SYNOPSIS
xargs [-0oprt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]
[-J replstr] [-L number] [-n number [-x]] [-P maxprocs] [-s size]
[utility [argument ...]]
DESCRIPTION
The xargs utility reads space, tab, newline and end-of-file delimited
strings from the standard input and executes utility with the strings as
arguments.
Any arguments specified on the command line are given to utility upon
each invocation, followed by some number of the arguments read from the
standard input of xargs. This is repeated until standard input is
exhausted.
Spaces, tabs and newlines may be embedded in arguments using single
(`` ' '') or double (``"'') quotes or backslashes (``\''). Single quotes
escape all non-single quote characters, excluding newlines, up to the
matching single quote. Double quotes escape all non-double quote
characters, excluding newlines, up to the matching double quote. Any
single character, including newlines, may be escaped by a backslash.
사용 예제
실행하는 스크립트나 프로그램에 인수들을 전달 할 때 사용하니까
실행 파일을 하나 만들어서 써보자
threewords.sh 라는 스크립트 실행 파일이 있다.
#!/bin/bash
if [[ "$#" != 3 ]]; then
echo "[Error] Three arguments required."
exit 1
fi
echo "The 1st argument is $1"
echo "The 2nd argument is $2"
echo "The 3rd argument is $3"
예제1 ) 스크립트에 인자로 바로 넘겨주기
$ echo "Tom likes Jerry" | xargs ./threewords.sh
결과
The 1st argument is Tom
The 2nd argument is Likes
The 3rd argument is Jerry
예제2) 인자를 넘겨 줄때 bash 명령어 활용 (변수를 지정해 넘기기)
# 빈칸 기준으로 split 되어 전달된다.
# $0 $1 $2
$ echo "Tom Likes Jerry" | xargs bash -c './threeOptions.sh $2 $1 $0'
결과
The 1st argument is Jerry
The 2nd argument is Likes
The 3rd argument is Tom
참고
redhat xargs man pages
'OS > Centos(redhat)' 카테고리의 다른 글
[CentOS] vim 세팅 (options) (0) | 2024.02.19 |
---|---|
[CentOS] chage 명령어 (패스워드 사용 만기 관리) (2) | 2024.02.18 |
[CentOS] systemd 서비스 등록 (0) | 2024.02.17 |
[CentOS] CentOS8 yum Cannot preprare internal mirrorlist (0) | 2024.02.16 |
[CentOS] 실행 중인 서비스 확인 방법 (0) | 2024.02.14 |