How to create a file using a variable as a file name?

I am testing Android mobile devices, and I would like to redirect the device log to a file whose name indicates the date and time of my test, and the device model being tested. For the first question, I have already decided with

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 that file name.

As for the device model, I wrote two bash lines that get it from the adb shell, namely

 device=$(adb shell cat /system/build.prop | grep "^ro.product.device=") deviceshortname=$(echo $device | sed 's/ro.product.device=//g') 

(not optimal since I'm not very good at bash programming ... :), but I manage to get

 $ echo $deviceshortname LT15i 

My problem is how to combine $now and $deviceshortname to get the file name, for example: LT15i_Jan_03_2012_13_19

I tried to set another variable:

 filename=($(echo $deviceshortname"_"$now)) 

and received:

 $ echo $filename LT15i_Jan_03_2012_13_19 

but if I try to redirect the log: $ adb logcat | tee $ filename

I get a file like this:

 -rw-r--r--+ 1 ele None 293 Jan 3 13:21 ?[01;31m?[K?[m?[KLT15i_Jan_03_2012_13_19 

I do not know why these strange characters and what I am doing wrong.

+4
source share
4 answers

Something adds color to your output. It can be grep(1) , it can adb , it can be baked in the /system/build.prop resource that you are reading.

If you're lucky, it is added by grep(1) - because it is very easy to disable it with --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 colors are added by adb , perhaps he has a command line option that asks him to avoid coloring the output.

If the colors are hardcoded to the /sys/build.prop resource in some way, then you will need a small tool that filters out color codes. I don't have one convenient (and this is sleep time), but you can probably create one starting with tr(1) to remove the \033 ASCII ESC characters.

+2
source

Similar to the ANSI sequence used by adb for color printing. A.

+2
source

I'm not sure if I am missing something, but it works for me.

p1 = Foo p2 = $ (date +% d_% m_% Y)

cat sample_file.txt | tee $ p1 "_" $ p2

0
source

Just type: echo ${deviceshortname}${now} and it will do the trick.

0
source

Source: https://habr.com/ru/post/1389064/


All Articles