bash 如何使用变量作为文件名创建文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8712260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a file using a variable as filename?
提问by Ele
I'm testing mobile Android devices and I would like to redirect the device log on a file whose name indicates both the date and time of my test, and the device model that is being tested. For the first issue, I have already resolved with
我正在测试移动 Android 设备,我想将设备日志重定向到一个文件,该文件的名称指示我的测试日期和时间以及正在测试的设备型号。对于第一个问题,我已经解决了
now=$(date +"%b_%d_%Y_%k_%M");adb logcat -c;adb logcat|tee $now
So:
所以:
$ echo $now
Jan_03_2012_13_09
and the tee command creates a file with this filename.
并且 tee 命令用这个文件名创建一个文件。
As for the device model I have written two bash lines that obtain it from adb shell, namely
至于设备模型,我写了两行从 adb shell 获取它的 bash 行,即
device=$(adb shell cat /system/build.prop | grep "^ro.product.device=")
deviceshortname=$(echo $device | sed 's/ro.product.device=//g')
(not optimal as I am not very good in bash programming... :) but I manage to get
(不是最佳的,因为我不太擅长 bash 编程...... :) 但我设法得到
$ echo $deviceshortname
LT15i
My problem is how to combine $nowand $deviceshortnameto obtain a filename such as:
LT15i_Jan_03_2012_13_19
我的问题是如何组合$now和$deviceshortname获取文件名,例如:
LT15i_Jan_03_2012_13_19
I tried to set another variable:
我试图设置另一个变量:
filename=($(echo $deviceshortname"_"$now))
and got:
并得到:
$ echo $filename
LT15i_Jan_03_2012_13_19
but if I try redirecting the log: $ adb logcat | tee $filename
但是如果我尝试重定向日志: $ adb logcat | 三通$文件名
I obtain such file:
我得到这样的文件:
-rw-r--r--+ 1 ele None 293 Jan 3 13:21 ?[01;31m?[K?[m?[KLT15i_Jan_03_2012_13_19
I don't know why these strange characters and what I'm doing wrong.
我不知道为什么这些奇怪的字符以及我做错了什么。
采纳答案by sarnold
Something is adding color to your output. It might be grep(1), it might adb, it might be baked into the /system/build.propresource that you're reading.
有些东西正在为您的输出添加颜色。它可能是grep(1),它可能adb,它可能会融入/system/build.prop您正在阅读的资源中。
If you're lucky, it is being added by grep(1)-- because that is supremely easy to disable with --color=no:
如果幸运的话,它是由grep(1)--添加的,因为使用以下命令非常容易禁用--color=no:
device=$(adb shell cat /system/build.prop | grep --color=no "^ro.product.device=")
deviceshortname=$(echo $device | sed 's/ro.product.device=//g')
If the colors are being added by adb, then perhaps it has a command line option that asks it to avoid colorizing the output.
如果颜色是由 添加的adb,那么它可能有一个命令行选项,要求它避免对输出进行着色。
If the colors are hard-coded into the /sys/build.propresource in some way, then you'll need some little tool that filters out the color codes. I don't have one handy (and it's bedtime) but you can probably build one starting with tr(1)to delete \033ASCII ESCcharacters.
如果颜色/sys/build.prop以某种方式硬编码到资源中,那么您将需要一些小工具来过滤掉颜色代码。我没有一个方便的(而且是就寝时间),但您可以构建一个从tr(1)删除\033ASCIIESC字符开始的。
回答by choroba
Looks like an ANSI sequence used by adbto color the output.
看起来像用于adb为输出着色的ANSI 序列。
回答by Mattias Ahnberg
Just type: echo ${deviceshortname}${now}and it will do the trick.
只需输入:echo ${deviceshortname}${now}它就可以解决问题。
回答by Vernon
I'm not sure if I'm missing something, but this works for me
我不确定我是否遗漏了什么,但这对我有用
p1=foo p2=$(date +%d_%m_%Y)
p1=foo p2=$(日期+%d_%m_%Y)
cat sample_file.txt | tee $p1"_"$p2
cat sample_file.txt | 三通$p1"_"$p2

