명령 결과 필터링

명령 결과 필터링

프로그램 내에서 다음 명령을 실행할 때 작은 프로그램을 작성하는 데 문제가 있습니다.

gsettings get org.gnome.desktop.background picture-uri

나는 얻다:

'file:///home/thamara/.config/variety/Favorites/wallpaper-2448037.jpg'

하지만 이는 다음과 같아야 합니다.

/home/thamara/.config/variety/Favorites/wallpaper-2448037.jpg

그렇지 않으면 프로그램이 실행되지 않습니다. 누구든지 나를 도와줄 수 있나요?

프로그램:

#!/bin/bash
## Blogry for GDM - Blurs your current wallpaper and puts it on your loginscreen

## Some Settings
effect='0x8' # Change this to anything you like http://www.imagemagick.org/Usage/blur/
save='~/Pictures/blur.jpg'

## Step one -  getting the current wallpaper
dir=$(gsettings get org.gnome.desktop.background picture-uri)

## Step two - Blurring the wallpaper
convert $dir -blur $effect -blur $effect -blur $effect $save

## Step three - exit (Since GDM automatically loads the new wallpaper there is no need for seting it.)
exit 0

## Links:
# http://www.imagemagick.org/Usage/blur/

답변1

명령의 출력이 gsettings일치합니다.GVariant 텍스트 구문. 이 매개변수는, 작은따옴표로 인쇄하세요.

따옴표를 제거해야 합니다.

dir_uri=$(gsettings get org.gnome.desktop.background picture-uri |
          sed "s/^'//; s/'\$//; s/\\\\\\(.\\)/\\1/")

그러면 URI를 얻게 됩니다. 파일 이름에 특수 문자가 포함되어 있지 않으면 file://시작 부분( file:)만 제거하세요.

dir=$(gsettings get org.gnome.desktop.background picture-uri |
      sed "s~^'file://~~; s~'\$~~")

답변2

Ubuntu 12.10에서 이 명령을 실행하면 다음과 같은 결과가 나타납니다.

$ dir=$(gsettings get org.gnome.desktop.background picture-uri)
$ echo $dir
'file:///usr/share/backgrounds/warty-final-ubuntu.png'

다음과 같이 저장된 값을 정리하면 됩니다 $dir.

$ dir="${dir/file:\/\//}"
$ echo $dir
'/usr/share/backgrounds/warty-final-ubuntu.png'

file://이렇게 하면 문자열이 처음부터 잘립니다. 다른 결과가 나타나면 이 설정을 변경할 수 있습니다. 귀하의 경우:

$ dir="${dir/\/\//}"

세부 사항

위의 내용은 패턴 대체를 사용하여 ${var/pattern/}변수에서 패턴을 제거합니다.pattern$var

대안

@jthill은 또한 Bash의 "일치 패턴 접두사 제거" 표기법을 사용하는 좋은 제안을 했습니다. 제 생각에는 이해하기가 조금 더 어렵지만 똑같이 효과적입니다.

$ dir="\'${dir#\'file://}"

\'file://위의 내용 은 에서 접두사를 제거한 다음 체크 표시로 바꾸고 $dir나머지 '는 .$dir'file://

배쉬 매뉴얼 페이지

Bash의 이러한 기능에 대해 더 자세히 알고 싶다면 그렇게 하시기를 권장합니다. 위에서 사용한 함수들입니다.

Bash 매뉴얼 페이지에서

${parameter#word}
${parameter##word}
       Remove matching prefix pattern.  The word is expanded to produce a 
       pattern just as in pathname expansion.  If the pattern matches the 
       beginning of  the  value  of  parameter, then  the  result  of  the  
       expansion is the expanded value of parameter with the shortest 
       matching pattern (the ``#'' case) or the longest matching pattern 
       (the ``##'' case) deleted.  If parameter is @ or *, the pattern 
       removal operation is applied to each positional parameter in turn, 
       and the expansion is the resultant list.  If parameter is  an array 
       variable subscripted with @ or *, the pattern removal operation is 
       applied to each member of the array in turn, and the expansion is the 
       resultant list.

${parameter/pattern/string}
       Pattern substitution.  The pattern is expanded to produce a pattern 
       just as in pathname expansion.  Parameter is expanded and the longest 
       match of pattern against  its  value is replaced with string.  If 
       pattern begins with /, all matches of pattern are replaced with 
       string.  Normally only the first match is replaced.  If pattern 
       begins with #, it must match at the beginning of the expanded value 
       of parameter.  If pattern begins with %, it must match at the end of 
       the expanded value of parameter.  If  string  is  null, matches  of  
       pattern  are  deleted  and the / following pattern may be omitted.  
       If parameter is @ or *, the substitution operation is applied to each 
       positional parameter in turn, and the expansion is the resultant 
       list.  If parameter is an array variable subscripted with @ or *, the 
       substitution operation is applied to each member of  the  array in 
       turn, and the expansion is the resultant list.

후속 질문 #1

OP는 아래 댓글에서 다음 질문을 했습니다.

이제 다음 문제에 직면했습니다. '/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg'' 이미지를 열 수 없습니다.

눈치채셨다면, 문제는 문자열 끝에 2개의 틱이 있다는 것입니다. 이것이 왜 거기 있는지는 모르겠지만 뒤에 오는 틱을 제거하려면 sed이전에 제공한 대체 명령 바로 다음에 이 명령을 사용할 수 있습니다. Bash의 교체 기능을 사용하여 마지막에 2개의 단일 틱을 처리하는 방법을 찾을 수 없습니다.

dir=$(echo "$dir" | sed "s/''/'/")

$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg''

$ dir=$(echo "$dir" | sed "s/''/'/")
$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg'

답변3

//파일 이름 시작 부분에 있는 여분의 두 개를 제거하고 ''이상하게도 파일 이름 끝에 있는 것을 제거하고 싶다면 다음을 사용할 수 있습니다 sed.

dir=$(gsettings get org.gnome.desktop.background picture-uri|sed -e 's/\/\/\//\//' -e "s/'//g; s/^/'/; s/$/'/")

이렇게 하면 잘못된 인스턴스나 중복 항목이 교체 ///되고 제거됩니다 . 마지막으로 파일 이름에 공백이 있으면 줄의 시작과 끝에 공백을 추가하여 파일 이름을 적절하게 캡슐화합니다. 이 솔루션은 약간 길지만 작업을 올바르게 수행해야 합니다./''

나도 뭔가를 울릴 것이다지속 가능한 개발 관리언급하고 말씀드리자면, 제가 달릴 때:

gsettings get org.gnome.desktop.background picture-uri

내가 얻는 결과는 다음 형식입니다.

'file:///usr/share/backgrounds/warty-final-ubuntu.png'

관련 정보