GVKun编程网logo

在bash中使用命令行utilsparsingvariables数组(bash命令用法)

26

如果您想了解在bash中使用命令行utilsparsingvariables数组和bash命令用法的知识,那么本篇文章将是您的不二之选。我们将深入剖析在bash中使用命令行utilsparsingva

如果您想了解在bash中使用命令行utilsparsingvariables数组bash命令用法的知识,那么本篇文章将是您的不二之选。我们将深入剖析在bash中使用命令行utilsparsingvariables数组的各个方面,并为您解答bash命令用法的疑在这篇文章中,我们将为您介绍在bash中使用命令行utilsparsingvariables数组的相关知识,同时也会详细的解释bash命令用法的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

在bash中使用命令行utilsparsingvariables数组(bash命令用法)

在bash中使用命令行utilsparsingvariables数组(bash命令用法)

要获取正在播放的当前歌曲(使用DBus ),我使用以下命令:

dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'

现在,下面的命令的输出如下所示:(为了清楚起见,简化了)

变体数组[

字典条目(

string“xesam:艺术家”

变体数组[

string“黑钥匙”

]

字典条目(

string“xesam:title”

变体string“给你的心”

]

输出有点长,我想从标题,艺术家等parsing它的具体价值。

谷歌search表示, 变种是一个stream行的数据types在组件对象模型中使用,所以我得到的是比使用sed / awk获取值更好的方法 。 那么,我该如何去做呢?

狂暴完成Maven逃脱冒号

重命名所有文件在Linux中的一次性

Linux sedreplacestring – 有人可以解释这个错误?

从工作目录运行UNIX程序?

wget:下载的文件名

从python运行代码具有不同于bash的权限?

如何将linux命令输出存储到puppet中的一个variables中

安装cygwin后Git Bash破碎

java Runtime.getRuntime()。exec()无法运行命令

find* .tar然后提取并删除文件

尝试这个

awk ' /string *"xesam:artist/{ while (1) { getline line if (line ~ /string "/){ sub(/.*string "/,"artist:",line) sub(/".*$/,"",line) print line break } } } /string *"xesam:title/{ while (1) { getline line if (line ~ /string "/){ sub(/.*string "/,"title:",line) print line break } } } ' variantArraySample.txt

给出输出

artist:The Black Keys title:Give Your Heart Away

这假设输入之间总是至少有一个线差,即

string "xesam:title" variant string "Give Your Heart Away"

也就是说,如果你的数据全部卷成一行,那么就需要更多的逻辑了

string "xesam:title" variant string "Give Your Heart Away"

(例如),将需要更改上述脚本。

让我知道如果你需要帮助进一步格式化您的需要输出。

我希望这有帮助。

下面的作品不管是什么顺序“标题”或“艺术家”进来。唯一的限制是,它看到:title ,包含string的下一行必须是标题字符串; 那线路距离多远并不重要。 同样的艺术家

awk ' /:artist/{a=1;next} /:title/{t=1;next} a && /string/{ sub(/^.*string /,"") artist=$0 a=0; next } t && /string/{ sub(/^.*string /,"") title=$0 t=0;next } END{ printf("artist:%sn title:%sn",artist,title) }'

产量

artist:"The Black Keys" title:"Give Your Heart Away"

$ {!variable:0:1}在bash中是什么意思?

$ {!variable:0:1}在bash中是什么意思?

假设您有以下这些变量:

foo=bar
bar=0123456789

代码

echo ${!foo}
0123456789

将为您提供$bar的价值!这称为间接扩展。这段代码:

echo ${bar:0:3}
012

将返回$bar的前3个符号,就像python中的slice一样。 现在,结合以下命令:

echo ${!foo:0:3}
012

我们得到$bar的前3个符号

bash中的对话框不能正确地获取variables

bash中的对话框不能正确地获取variables

我对这个脚本的一致性有点问题。 你可以运行一次,没有任何问题。 但是,如果立即将其加载并重新运行,则不会正确获取variables,因为输出不会以正确的方式写入文件。

例如:该实用程序旨在更新4个文件。 .temp,.tiers,.version和.sync通过对话提示的各个阶段,它会更新一个文件。 我的问题是,有些时候它不会更新文件,我不能find任何原因,因为它只有在你已经运行一次。 我在启动时擦除了文件,所以我不确定这个交易是什么。 这就像是从内存中抓取它?

无论如何,要testing这个,你需要在/ test下面的文件

感谢任何能给我一些指导的人。

Ncurses:对话框的背景颜色被更改?

WPF / C# – 对话框窗口不响应触摸

如何使Win32对话框出现在默认位置(CW_USEDEFAULT)?

对话框 – 没有构造函数

我如何编程添加一个项目到这个对话框?

cat .tiers Stable=1 Release=2 Beta=3

码:

#!/usr/bin/env bash touch .version touch .temp VERSION=`cat .version` DIR=/test/ STORED=`cat ${DIR}/.temp` ################################ # REARRANGE TIERS # ################################ rearrange() { start end } ################################ # SYNC FILE EXISTS # ################################ sync_exists() { dialog --msgBox ".sync exists in the directory $(echo ${VERSION}).nn Use Tier Move instead." 10 40 clean_up exit 1; } ################################ # CLEAN UP # ################################ clean_up() { rm -rf .version rm -rf .update rm -rf .temp } ################################ # OPTOUT & CLEAN UP # ################################ opt_out() { dialog --msgBox "You've opted out of the tier update." 5 40 rm -rf ${DIR}/.version ${DIR}/.update ${DIR}/.temp } ################################ # UPDATE .TIERS FILE # ################################ tier_updater() { dialog --msgBox "nn $(cat ${DIR}/.temp) is Now $VERSION." 8 27 sed -is/${STORED}=.*/${STORED}=${VERSION}/g ${DIR}/.tiers clean_up } ################################ # UPDATE FILE LIST # ################################ file_updater() { rm -rf ${VERSION}/.sync for x in `find $VERSION -type d ( ! -iname "." )|sed s/${VERSION}//g`; do echo "d===$x===755" >> ${DIR}/${VERSION}/.sync ; done for y in `find $VERSION -type f ( ! -iname ".*" )|sed s/${VERSION}//g`; do echo "f===$y===644===`md5sum ${VERSION}"$y"|awk '{print $1}'`" >> "${DIR}"/"$VERSION"/.sync;done find "${DIR}"/"${VERSION}" -type f -exec gzip -f '{}' ; > /dev/null 2>&1 |xargs gzip -d "${DIR}"/${VERSION}/.sync } ################################ # TIER UPDATE PROMPT # ################################ tier_update_prompt() { if [ -f ${VERSION}/.sync ]; then sync_exists else file_updater fi dialog --menu "n Directory List Built.n File List Built.n Files Compressed.n Sync File in place.nn Would you like to update the tier for $(cat ${DIR}/.temp)?" 15 60 2 "Yes" "This will apply all changes for $(echo $VERSION) to $(cat ${DIR}/.temp)." "No" "This will revert all changes." 2>&1 > ${DIR}/.update if [ "$?" = "0" ] ; then _update=$(cat ${DIR}/.update) if [ "$_update" = "Yes" ] ; then tier_updater fi if [ "$_update" = "No" ] ; then opt_out ; fi else echo "You have Now exited the application" clean_up; fi } ################################ # NEW VERSION INPUT # ################################ stable() { dialog --inputBox "Enter the new version for $(cat ${DIR}/.temp)" 8 30 2> "${DIR}"/.version if [ -d `cat .version` ] ; then tier_update_prompt; else dialog --msgBox "WARNING!!!!nn The folder $(${VERSION}) does not exist!nPlease upload this folder before you proceed!" 8 50 ; clean_up fi } ################################ # TIER SELECTION # ################################ startup() { dialog --menu "Tiers are currently set as the following. Which would you like to update? n" 12 78 5 "Stable" "$(cat ${DIR}/.tiers|grep Stable|sed 's/Stable=//g')" "Release" "$(cat ${DIR}/.tiers|grep Release|sed 's/Release=//g')" "Beta" "$(cat ${DIR}/.tiers|grep Beta|sed 's/Beta=//g')" 2> "${DIR}"/.temp # OK is pressed if [ "$?" = "0" ] ; then _return=$(cat ${DIR}/.temp) fi if [ "$_return" = "Stable" ] ; then stable fi if [ "$_return" = "Release" ] ; then stable fi if [ "$_return" = "Beta" ] ; then stable else echo "You have Now exited the application" clean_up; fi }

使用C ++ Win32 API禁用MessageBox右侧顶部的X-Button图标?

使用箭头键在Windows对话框中导航

为什么我的程序不能显示这个对话框,而另一个程序可以吗?

如何从C#启动windows“运行”对话框

Windows编程DialogBox背景图片

您可以使用该方法直接捕获对话框输入到变量中:

exec 3>&1 result=$(dialog --menu head 15 20 6 $(for ((i=1;i<30;i++));do echo tag$i item$i;done) 2>&1 1>&3); exitcode=$?; exec 3>&-; echo $result $exitcode

http://mywiki.wooledge.org/BashFAQ/002

所以我的方法startup将如下所示:

startup() { exec 3>&1 _return=$(dialog --menu "Tiers are currently set as the following. Which would you like to update? n" 12 78 5 "Stable" "$(cat ${DIR}/.tiers|grep Stable|sed 's/Stable=//g')" "Release" "$(cat ${DIR}/.tiers|grep Release|sed 's/Release=//g')" "Beta" "$(cat ${DIR}/.tiers|grep Beta|sed 's/Beta=//g')" 2>&1 1>&3) exitcode=$? exec 3>&-; # OK is pressed if [ "$exitcode" == "0" ] ; then if [[ "$_return" == "Stable" || "$_return" == "Release" || "$_return" == "Beta" ]] then stable fi fi echo "You have Now exited the application" clean_up; }

除此之外,还有许多不洁净的东西,例如$($ {VERSION}):

dialog --msgBox "WARNING!!!!nn The folder $(${VERSION}) does not

正如我从代码理解它将运行空的命令为.version在启动时是空的

VERSION=`cat .version`

在tier_update_prompt()$ VERSION中仍然是空字符串,

我试图重构你的代码,但它变成了很多建议:

得到对话结果,如上所示

使用参数运行函数:

#宣言

function_name(){

参数= $ 1;

回声“$参数”

}

VAR =值

#run函数

函数名称var

不要与$ {VAR}过度复杂,

请记住,双引号扩展,所以echo "here is $var"而不是echo "here is $(echo ${var})"

比如使用更少的临时文件: diff <(ls dir1) <(ls dir2)

不要在根目录下保存工作文件(如/ test /)

不要以root身份工作

学习perl

Bash中的局部variables像Perl中一样?

Bash中的局部variables像Perl中一样?

它错误地重复相同的string两次

grep -q '+::::::' /etc/passwd || echo '+::::::' >> /etc/passwd

但如果我这样做

{ local a='+::::::' local b="/etc/passwd" grep -q $a $b || echo $a >> $b }

bash抱怨

-bash: local: can only be used in a function

如何在多核上运行使用bash进行pipe理的进程?

用SED / Perl删除包含数字的string的部分

Linux bash Timer

如何仅听取inotifywait中的特定事件?

如何用sed中的空格replace单引号?

有没有办法在Bash中执行局部variables,类似于Perl如何处理{ ... } ?

最后运行不同的选项GNU并行

在使用–color = always选项时,grep结果会有不同的结果

是否可以在“if”语句中pipe多个命令?

如何评估bash shell中的给定path

bash脚本将string视为命令

对于您的具体示例,您可以使用一个子shell,它有效地本地化内部分配的所有变量。

( a='+::::::' b="/etc/passwd" grep -q "$a" "$b" || echo "$a" >> "$b" )

至少不要与{ ... } 。

高级Bash脚本指南指出用{ ... }创建的代码块创建一个anonymous function ,但是脚本的其余部分仍然可以看到所有变量。

chepner指出,这可能是错误的术语。

bash的man页面 ,称之为group command有以下说法:

{ list; } list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is kNown as a group command. The return status is the exit status of list. Note that unlike the Metacharacters ( and ),{ and } are reserved words and must occur where a reserved word is permitted to be recog‐ nized. Since they do not cause a word break,they must be separated from list by whitespace or another shell Metacharacter.

这里是一些更多的信息在本地变量和他们的范围。

Bash脚本帮助 – Tar不能使用variables?

Bash脚本帮助 – Tar不能使用variables?

我试图通过使用bash脚本和CRON任务来基于昨天的date来备份设置的文件夹。

该网站的文件夹结构是这样的:

/home/admin/domains/mysite.com/public_html/media/2014/march

而且我想把这个文件夹备份到这个文件中:

/home/admin/domains/mysite.com/public_html/bk/mediabackup-march-2014.tar.gz

所以创build了这个脚本:

用awk从两个文件中写出一个两列文件

使用curl或其他bash命令login到Apple Developer Console

删除string与sed的组合

linux shell支持列表数据结构吗?

如何预先在Unix中通过find命令find文件的最后一行

#!/bin/bash Now=$(date -d "12 hours ago" ''+%m'') NowYEAR=$(date -d "12 hours ago" ''+%Y'') MONTHS=(Dummy January February march April May June July August September October November December) NowMONTH=${MONTHS[3]} FILE="/home/admin/domains/mysite.com/public_html/bk/mediabackup-$NowMONTH-$NowYEAR.tar.gz" PATH="/home/admin/domains/mysite.com/public_html/media/$NowYEAR/$NowMONTH" tar -zcvf $FILE $PATH

当我通过shell运行这个脚本时,我会这样做:

bash script.sh

它可能会出现“命令未find” – 不知道为什么它不工作?

任何帮助将是伟大的,谢谢:)

Bash文件显示“ln:command not found”

如何通过bash上的文件types(使用“文件”命令)将文件分类到文件夹中?

用于循环多个文件夹中的文件 – bash shell

如何在bash脚本中使用awk过滤两个date之间的数据

unix shell脚本中出现分段错误(core dumped)错误。 帮助发现错误?

通过重写shell的内置PATH变量,导致它找不到tar命令。 使用另一个变量名称,通常不要使用大写变量名称。

不需要硬编码月份名称:

read year month < <(date -d "12 hours ago" "+%Y %B") echo "$month-$year"

march-2014

总结

以上是小编为你收集整理的Bash脚本帮助 – Tar不能使用variables?全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

关于在bash中使用命令行utilsparsingvariables数组bash命令用法的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于$ {!variable:0:1}在bash中是什么意思?、bash中的对话框不能正确地获取variables、Bash中的局部variables像Perl中一样?、Bash脚本帮助 – Tar不能使用variables?等相关知识的信息别忘了在本站进行查找喔。

本文标签: