2011-10-11 2 views
31

나는 bash는 스크립트에서이 같은 긴 명령을 포함하고 싶습니다 : 헤어 졌 긴 파일 이름bash에서 매우 긴 문자열 리터럴을 어떻게 분할합니까?

mycommand \ 
    --server myserver \ 
    --filename extremely/long/file/name/that/i/would/like/to/be/able/to/break/up/if/possible \ 
    --otherflag \ 
    --anotherflag 

합니다.

# Insufficiently pretty 
mycommand \ 
    --server myserver \ 
    --filename extremely/long/file/name/\ 
that/i/would/like/to/be/able/to/break/\ 
up/if/possible \ 
    --otherflag \ 
    --anotherflag \ 

을하지만 흐름을 나누기 :

나는이 작업을 수행 할 수 있습니다. 나는 처럼이 쓸 수 있어야하는 것입니다 :

# Doesn't work 
mycommand \ 
    --server myserver \ 
    --filename extremely/long/file/name/\ 
     that/i/would/like/to/be/able/to/break/\ 
     up/if/possible \ 
    --otherflag \ 
    --anotherflag 

을하지만 리터럴 문자열을 나누기 때문 작동하지 않습니다.

bash에 문자열 리터럴을 위반하지만 앞에 오는 공백을 무시하도록하는 방법이 있습니까?

답변

36

당신이 변수를 사용할 수 있습니다

file=extremely/long/file/name 
file+=/that/i/would/like/to/be/able/to/break 
file+=/up/if/possible 

mycommand\ 
    --server myserver\ 
    --filename $file\ 
    --flag flag 
+6

좋은 아이디어. 'file = $ {file}/... '대신에'+ ='연산자를 사용하여 더 깔끔하게 만들 수 있습니다. – Chriszuma

+5

+1 : 이것은 제 의견으로는 가장 모호한 접근법입니다. 그 의도는 분명합니다. –

+0

@Chriszuma 그렇습니다. 배쉬가'+ = '연산자를 허용한다는 것을 잊었습니다. 나는 내 대답을 편집했다. – WilQu

36

그것은 해킹의 비트,하지만이 작동합니다

mycommand \ 
    --server myserver \ 
    --filename "extremely/long/file/name/"` 
       `"that/i/would/like/to/be/able/to/break/"` 
       `"up/if/possible" \ 
    --otherflag \ 
    --anotherflag 

배쉬는 인접한 문자열 리터럴을 연결, 그래서 우리는 그 활용합니다. 예를 들어 echo "hi" "there"hi there을 인쇄하고 echo "hi""there"hithere을 인쇄합니다.

백틱 연산자와 공백이 아무 것도 계산되지 않는다는 사실을 이용합니다.

+2

'\'라인을 대신하여 앞의 줄 끝에'\'을 열 수 있습니다. 왼쪽 가장자리를 청결하게 유지합니다. –

+0

좋은 전화는 생각하지 않았습니다. 편집 Duly. – Chriszuma

1

기본적으로,이 작업을 수행하는 떠들썩한 파티에 내장 아무것도 없다.
래퍼는 일반적으로 가치가있는 것보다 문제가 많지만, 별칭이나 기능을 시도 할 수 있다고합니다.

j(){sed -e ':a;$!N;s/ *\n *//g;ta' <<<"$1"} 

echo "$(j "3 spaces 
      /hello 
      /world 
      /this 
      /is 
      /a 
      /long 
      /path 
      ")" 

# 3 spaces/hello/world/this/is/a/long/path 
3

j 나는 내 bash는 스크립트의 상단에 짧은 strcat와 함수를 정의하고 물건을 분할 인라인 호출을 사용합니다. 명령 호출과 함께 긴 리터럴을 정의 할 수 있으므로 별도의 변수를 사용하는 것을 선호합니다.

function strcat() { 
    local IFS="" 
    echo -n "$*" 
} 

mycommand \ 
    --server myserver \ 
    --filename "$(strcat \ 
     extremely/long/file/name/ \ 
     that/i/would/like/to/be/able/to/break/ \ 
     up/if/possible)" \ 
    --otherflag \ 
    --anotherflag \ 

나는 또한 내가 값 사이에 쉼표를 입력하지 않도록하는 데 사용할 수 있기 때문에 플래그 매개 변수로 값의 긴 CSV를 입력해야하는 경우에이 방법 같은 :

function strjoin() { 
    local IFS="$1" 
    shift 
    echo -n "$*" 
} 

csv_args=(
    foo=hello 
    bar=world 
    "this=arg has spaces in it" 
) 
mycommand \ 
    --server myserver \ 
    --csv_args "$(strjoin , "${csv_args[@]}")" \ 
    --otherflag \ 
    --anotherflag \ 

동일합니다

mycommand \ 
    --server myserver \ 
    --csv_args "foo=hello,bar=world,this=arg has spaces in it" \ 
    --otherflag \ 
    --anotherflag \ 
1

하나 또한 가변 어레이를 사용할 수

file=(extremely/long/file/name 
    /that/i/would/like/to/be/able/to/break 
    /up/if/possible) 
IFS='' 

echo mycommand\ 
    --server myserver\ 
    --filename "${file[*]}"\ 
    --flag flag 
,745,
관련 문제