0

i searched the forum and stumbled across some relevant questions but none of the answers working for me

im playing around with bash scripting, adb and want to get some basic informations from the mobile phone

sdk_version=$(adb shell getprop ro.build.version.sdk)
sdl_version=$(adb shell getprop ro.build.version.sdl)
android=$(adb shell getprop ro.build.version.release)
model=$(adb shell getprop ro.product.model)
name=$(adb shell getprop ro.product.name)
serial=$(adb shell getprop ril.serialnumber)

printf 'SDK: %s\n' $sdk_version
printf 'SDL: %s\n' $sdl_version
printf 'Product: %s\n' $model $name (i think this version was working, but results in two seperate lines with Product:)

well this is quite simple and works without problems, one line, one var, but when i want to concat 2 of these vars like model and name in one line, the result is an "overwritten line" when outputting the result looks like "xcover 39F" (first string is partly overwritten), the desired result should be a string like "SM-G389F xcover3"

what i tried so far after reading some other questions here

concat with the help of a thirdvar like

vara="a"
varb="b"
varc="%s %s"
printf "$varc" $vara $varb

or

vara="a"
varb="b"
varc="${vara}${varb}"
printf '%s\n' $varc

using mostly all combinations with/without curly brackets but nothing results in the desired output, the output is empty or fully/partly overwritten

what i need is a possibility to concat 2 or more of the above output vars, as one string

  • Did you check for embedded carriage returns in the variables? This would explain the overwriting. – user1934428 Jul 22 '20 at 10:28
  • how to achieve this? i think this is the problem – Lecker Spam Jul 22 '20 at 10:35
  • While `tr` could be used to remove unwanted characters, have a look first where they are (you can use `xxd` for this) and how they came in in the first place. Maybe you got the variables from a file which had CRLF line endings; in this case, consider running `dos2unix` to remove them in the original file. It's hard to give a general recommendation if we don't know what exactly you want to do with the data. If you have CRs and need to keep them, think of *how* you then would like to have displayed tthe content of the variables. – user1934428 Jul 23 '20 at 06:02
  • BTW, the whole problem really applies only to displaying the variables. Concatenation is done correctly by you, as you can verify by doing a `xxd <<<"$varc"` instead of your `printf`. – user1934428 Jul 23 '20 at 06:04

3 Answers3

1
vara="a"
varb="b"
varc="$vara $varb"
echo $varc

This will provide the required output.

Also if you use printf put quotes between variable ("$varc") to treat it as a single string, otherwise string will be split (space will be replaced).

printf "%s\n" "$varc"
j23
  • 3,139
  • 1
  • 6
  • 13
  • not working, result is overwritten, please be aware, that my vara, varb are results from an adb command see above – Lecker Spam Jul 22 '20 at 10:36
1

How to remove carriage return and newline from a variable in shell script

using %q in printf i was able to verify, that the outputvars look like 'SM-G389G\r' so the problem is really the carriage return as suggested by user1934428

i think i can handle it from here

EDIT: cleaning my vars first with

testVar=${testVar//$'\r'}

and the printf statements or variable concat working as expected

thread can be closed

0

Here's how printf %s works

a=1 b=2 c='3 4'

$ printf '|%s|\n' $a
|1|

$ printf '|%s|\n' $a $b
|1|
|2|

$ printf '|%s|\n' $a $b $c
|1|
|2|
|3|
|4|

$ printf '|%s|\n' $a $b "$c"
|1|
|2|
|3 4|

$ printf '|%s|%s|\n' $a $b
|1|2|

$ printf '|%s|%s|%s|\n' $a $b $c
|1|2|3|
|4|||

$ printf '|%s|%s|%s|\n' $a $b "$c"
|1|2|3 4|

Think it's pretty straightforward

Ivan
  • 6,188
  • 1
  • 16
  • 23
  • in this case, it is also overwritten, please be aware, that my vara, varb are results from an adb command see above – Lecker Spam Jul 22 '20 at 10:37