查看bash变量的确切内容? (hexdump没有帮助) [英] See exact content of bash variable? (hexdump doesn't help)

查看:125
本文介绍了查看bash变量的确切内容? (hexdump没有帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题在下面,但我的问题是更通用的 - 如何看到由bash变量引用的内存的确切内容,以了解它们为什么不匹配:

 #[[$ c1 == $ c]] || echo nope 
nope
#[[$ c1 ==bkup dt]] || echo nope
#[[$ c ==bkup dt]] || echo nope
nope
#hexdump -C <<< $ c
00000000 62 6b 75 70 20 64 74 0a | bkup dt。|
00000008
#hexdump -C <<<$ c1
00000000 62 6b 75 70 20 64 74 0a | bkup dt。|
00000008
#[$ c1=$ c] || echo nope
nope
#[! $ c1=$ c] || echo nope

或者它看起来像一个错误?我可以重复这个问题:

  $ cd / tmp 
$ mkdir aaa
$ echo 2> ; aaa / 1
$ echo 2> aaa / 2
$ c = $(ls -A aaa)
$ [[$ c == $(echo $ c)]] || echo不等于
不等于
$ hexdump -C <<< $ c
00000000 31 20 32 0a | 1 2. |
00000004
$ hexdump -C <<< $(echo $ c)
00000000 31 20 32 0a | 1 2. |
00000004
$ c1 =1 2
$ [[$ c1 == $(echo $ c1)]] ||回声不等于
$ [[$ c1 == $(echo $ c)]] ||回声不等于
$ [[$ c1 == $ c]] ||回声不等于
不等于


解决方案

检查变量内容的方法是使用 declare -p

  $ c =1 2
$ declare -pc
declare - c =1 2

请注意,您的测试是错误的,因为您错过了变量扩展中的引号!



查看:

  $ c =1 2#有两个空格
$ declare -pc
declare - c =1 2
$ echo $ c
1 2
$ echo$ c
1 2
$ d = $(echo $ c)
$ declare - pd
1 2

您必须引用每一个变量扩展,除非您真的希望将分词和路径名扩展应用于他们!(通常,您当然不希望发生这种情况)。

code> hexdump 您需要引用的策略:

  $ c =1 2#两个空格
$ hexdum p<< $ c
00000000 31 20 32 0a | 1 2. |
00000004
$ hexdump<<< $ c
00000000 31 20 20 32 0a | 1 2. |
00000005

您遇到的情况正是如此:

  $ mkdir aaa; touch aaa / {1,2} 
$ c = $(ls -A aaa)
$ declare -pc
declare - c =1
2
$#见?这些文件之间有一条新的线。
$ echo $ c
1 2
$ echo$ c
1
2
$#使用引号!






有时 declare -p 不会正确显示空格。在这种情况下,您可以使用 printf ,例如:

  $ c = $'$ \\ n'
$ declare -pc
declare - c =

$#有空格,但是看不到它们
$ printf'%q\\\
'$ c
$'\\\
'



< hr>

声明策略也很好,因为您也可以检查数组和函数:

  $ a =(one twothree four)
$ declare -pa
declare -aa ='([0] =one[1] =two[2] =三个四个)'
$ declare -A h =([one] = 1 [two] = 2)
$ declare -ph
declare -A h ='([one] = 1[two] =2)'
$ f(){echo hello; }>某处> >(over the rainbow)
$ declare -pf f
f()
{
echo hello
}>某处2> >(over the rainbow)
$#您还需要-f开关来定位函数

您还可以访问变量的标志:

  $ declare -litux hello = world 
$ declare -p hello
declare -itx hello =0
$#玩得开心!


So I have a problem below, but my question is more generic - how to see exact content of memory referenced by bash variable to understand why they don't match:

# [[ $c1 == $c ]] || echo nope
nope
# [[ $c1 == "bkup dt" ]] || echo nope
# [[ $c == "bkup dt" ]] || echo nope
nope
# hexdump -C <<<$c
00000000  62 6b 75 70 20 64 74 0a                           |bkup dt.|
00000008
# hexdump -C <<<$c1
00000000  62 6b 75 70 20 64 74 0a                           |bkup dt.|
00000008
# [ "$c1" = "$c" ] || echo nope
nope
# [ ! "$c1" = "$c" ] || echo nope

Or does it look like a bug? I can repeat the problem with:

$ cd /tmp
$ mkdir aaa
$ echo 2 > aaa/1
$ echo 2 > aaa/2
$ c=$(ls -A aaa)
$ [[ $c == $(echo $c) ]] || echo not equal
not equal
$ hexdump -C <<<$c
00000000  31 20 32 0a                                       |1 2.|
00000004
$ hexdump -C <<<$(echo $c)
00000000  31 20 32 0a                                       |1 2.|
00000004
$ c1="1 2"
$ [[ $c1 == $(echo $c1) ]] || echo not equal
$ [[ $c1 == $(echo $c) ]] || echo not equal
$ [[ $c1 == $c ]] || echo not equal
not equal

解决方案

The best thing to inspect the content of a variable is to use declare -p:

$ c="1 2"
$ declare -p c
declare -- c="1 2"

Note that your tests are wrong because you're missing out quotes in your variable expansions!

Look:

$ c="1  2" # with two spaces
$ declare -p c
declare -- c="1  2"
$ echo $c
1 2
$ echo "$c"
1  2
$ d=$(echo $c)
$ declare -p d
1 2

You must quote every single variable expansion, unless you really want to have word splitting and pathname expansion applied to them! (and usually, you certainly don't want that to happen).

Even with your hexdump strategy you need quotes:

$ c="1  2" # two spaces
$ hexdump <<< $c
00000000  31 20 32 0a                                       |1 2.|
00000004
$ hexdump <<< "$c"
00000000  31 20 20 32 0a                                    |1  2.|
00000005

What you're experiencing is exactly this:

$ mkdir aaa; touch aaa/{1,2}
$ c=$(ls -A aaa)
$ declare -p c
declare -- c="1
2"
$ # see? there's a new line between the files.
$ echo $c
1 2
$ echo "$c"
1
2
$ # Use quotes!


Sometimes declare -p will not quite show spaces properly. In this case you can use printf like so:

$ c=$'    \n'
$ declare -p c
declare -- c="    
"
$ # there are spaces, but you can't see them
$ printf '%q\n' "$c"
$'    \n'


The declare strategy is also nice as you can inspect arrays and functions too:

$ a=( one two "three four" )
$ declare -p a
declare -a a='([0]="one" [1]="two" [2]="three four")'
$ declare -A h=( [one]=1 [two]=2 )
$ declare -p h
declare -A h='([one]="1" [two]="2" )'
$ f() { echo hello; } > somewhere > >(over the rainbow)
$ declare -pf f
f () 
{ 
    echo hello
} > somewhere 2> >(over the rainbow)
$ # You need also the -f switch to target functions

You also have access to the flags of the variables:

$ declare -litux hello=world
$ declare -p hello
declare -itx hello="0"
$ # Have fun!

这篇关于查看bash变量的确切内容? (hexdump没有帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆