过滤命令的结果

过滤命令的结果

我在编写小程序时遇到困难;当我从程序中运行以下命令时:

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 这些功能的更多信息,我鼓励您这样做。这些是我们上面使用的功能。

摘自 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'

相关内容