Bash에서 여러 줄 문자열을 출력하려면 어떻게 해야 합니까?
다음과 같이 여러 에코 콜을 사용하지 않고 Bash에서 멀티 라인 문자열을 출력하려면 어떻게 해야 합니까?
echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
Bash 빌트인만 사용하여 휴대할 수 있는 방법을 찾고 있습니다.
여기서 문서는 이러한 목적으로 자주 사용됩니다.
cat << EOF
usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to:
up home page:
EOF
모든 버전의 Bash를 포함하여 Bourne에서 파생된 모든 쉘에서 지원됩니다.
또는 다음과 같이 할 수 있습니다.
echo "usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to:
up home page: "
이 페이지의 통찰력 있는 답변에 영감을 받아 혼합된 접근방식을 만들었습니다.이 접근방식은 가장 심플하고 유연하다고 생각합니다.당신은 어떻게 생각하나요?
먼저 변수에서 용도를 정의합니다.이를 통해 다른 컨텍스트에서 재사용할 수 있습니다.형식은 매우 간단하며 거의 WYSIWYG에 가깝습니다. 제어 문자를 추가할 필요가 없습니다.이 제품은 상당히 휴대성이 좋은 것 같습니다(MacOS와 Ubuntu에서 실행했습니다).
__usage="
Usage: $(basename $0) [OPTIONS]
Options:
-l, --level <n> Something something something level
-n, --nnnnn <levels> Something something something n
-h, --help Something something something help
-v, --version Something something something version
"
그럼 간단하게 사용할 수 있습니다.
echo "$__usage"
또는 파라미터를 해석할 때 한 줄에 에코할 수 있습니다.
levelN=${2:?"--level: n is required!""${__usage}"}
사용하다-e옵션에서 새 행 문자를 인쇄할 수 있습니다.\n현악기 안에.
예를 들어 다음과 같습니다.
echo -e "This will be the first line \nand this will be on the second line"
제가 추천했으니까printf코멘트에서는, 그 사용 예를 몇개 들겠습니다(사용 메세지 인쇄의 경우는, Dennis 나 Chris 의 답변을 사용하는 것이 좋습니다). printf사용법이 좀 더 복잡합니다.echo첫 번째 인수는 형식 문자열로, 이 문자열에서 이스케이프(예:\n)는 항상 해석됩니다.또, 다음의 형식 지시어를 포함할 수도 있습니다.%이 명령어는 추가 인수를 포함하는 위치와 방법을 제어합니다.다음으로 사용현황 메시지에 사용할 수 있는 두 가지 방법을 제시하겠습니다.
먼저 메시지 전체를 다음 형식 문자열에 포함할 수 있습니다.
printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: \nup home page: \n"
주의해 주세요.echo마지막 줄바꿈을 명시적으로 포함해야 합니다.또, 메세지에 다음의 정보가 포함되어 있는 경우%그 글자들은 다음과 같이 쓰여져야 한다.%%버그 리포트나 홈페이지의 주소를 포함시키고 싶은 경우는, 매우 자연스럽게 추가할 수 있습니다.
printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: %s\nup home page: %s\n" "$bugreport" "$homepage"
둘째, 형식 문자열을 사용하여 각 추가 인수를 별도의 행에 출력할 수 있습니다.
printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: " "up home page: "
이 옵션을 사용하면 버그 보고서와 홈페이지 주소를 추가할 수 있습니다.
printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: $bugreport" "up home page: $homepage"
또한 들여쓰기된 소스 코드를 사용하면(후행 대시 포함) 선행 탭(선행 공백은 제외)을 무시할 수 있습니다.
예를 들어 다음과 같습니다.
if [ some test ]; then
cat <<- xx
line1
line2
xx
fi
선행 공백 없이 들여쓰기된 텍스트를 출력합니다.
line1
line2
저는 보통 더 유연하고 직관적이라고 생각되는 내장된 read 명령어를 사용합니다.행의 내용을 변수로 읽어, 특수한 셸 변수 IFS에 관련 붙여진 단어 분할을 가능하게 합니다.상세한 것에 대하여는, 이 블로그나 man 페이지를 참조해 주세요.
read -r -d '' usage <<-EOF
usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to: $report server
up home page: $HOME
EOF
echo "$usage"
한 가지요.printf된 변수 전정정 ( (((((((((((((((((((((((:msg을 템플릿으로 을 사용하다
msg="First line %s
Second line %s
Third line %s
"
one='additional message for the first line'
two='2'
tri='this is the last one'
printf "$msg" "$one" "$two" "$tri"
은 ^^^ > > ^^ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >%s순서대로
You can write your
text
freely,
in a separate:
----file.
그리고 나서.
echo "$(</pathto/your_multiline_file)"
다음을 수행합니다.
dedent() {
local -n reference="$1"
reference="$(echo "$reference" | sed 's/^[[:space:]]*//')"
}
text="this is line one
this is line two
this is line three\n"
# `text` is passed by reference and gets dedented
dedent text
printf "$text"
을 사용한 : OUT OUT »dedent 번째 first:::
this is line one
this is line two
this is line three
전화와 함께...그리고 전화와 함께dedent첫 번째(위 그림 참조):
this is line one
this is line two
this is line three
상세한 것에 대하여는, 이 점에 대해 이미 기술하고 있는 것을 참조해 주세요.
그리고 물론 @Andreas Louv가 이 기능을 보여줘서 고마워요.
지금까지의 대처는 다음과 같습니다.
function help_text {
printf "\n\
Usage: ./cpanel-to-cc.sh [arguments] ... \n\
Examples: \n\
\t ./cpanel-to-cc.sh --client-id 123123 --api-key abc123def456 --domain example.com \n\
\t ./cpanel-to-cc.sh --client-id 123123 --tmp-dir /home/user/cpanel-to-cc \n\
\t ./cpanel-to-cc.sh --resync --domain example.com \n\
\t ./cpanel-to-cc.sh --purge \n\
\n\
Arguments: \n\
Option \t\t\t Long option \t\t\t Function \n\
-c <id> \t\t --client-id <id> \t\t Specify the SiteHost Client ID \n\
-k <key> \t\t --api-key <key> \t\t Specify the SiteHost API key with access to Cloud, Job and Server modules \n\
-d <domain> \t\t --domain <domain> \t\t The cPanel domain to migrate. If not specified we try migrate all \n\
-t <directory> \t --tmp-dir <directory> \t\t Directory to store temporary files and logs. Default is: $TMP_DIR \n\
-v \t\t\t --verbose \t\t\t Print debugging/verbose information \n\
-y \t\t\t --assume-yes \t\t\t Automatic yes to prompts. Assume \"yes\" as answer to all prompts \n\
-r \t\t\t --resync \t\t\t Use credentials stored and copy data into Container already created. \n\
-p \t\t\t --purge \t\t\t Remove any metadata stored on the the server. This removes any files in: $TMP_DIR \n\
-h \t\t\t --help \t\t\t Display this help and exit \n\
\n"
}
언급URL : https://stackoverflow.com/questions/10969953/how-to-output-a-multiline-string-in-bash
'programing' 카테고리의 다른 글
| Kotlin 목록과 배열 유형의 차이 (0) | 2023.04.20 |
|---|---|
| 열에 대한 이유가 집계 함수 또는 GROUP BY 절에 포함되어 있지 않으므로 선택 목록에 유효하지 않습니다. (0) | 2023.04.20 |
| 상호 또는 순환(순환) 가져오기를 사용하면 어떻게 됩니까? (0) | 2023.04.20 |
| 서버측 캐시를 사용하지 않고 cURL을 호출하려면 어떻게 해야 합니까? (0) | 2023.04.20 |
| Visual Studio 코드 터미널, 관리자 권한으로 명령을 실행하는 방법 (0) | 2023.04.20 |