xR34P3Rx
xR34P3Rx

Reputation: 395

funky file name output from shell/bash?

So, im making a small script to do an entire task for me. The task is to get the output of the dmidecode -Fn into a text file and then take a part of the dmidecode output, in my example, the Address (0xE0000) as the file name of the txt.

My script goes as follows and does work, i have tested it. The only little issue that i have, is that the file name of the txt appears as "? 0xE0000.txt"

My question is, why am i getting a question mark followed by a space in the name?

#!/bin/bash
directory=$(pwd)
name=$(dmidecode|grep -i Address|sed 's/Address://')
inxi -Fn > $directory/"$name".txt

The quotes in the "$name".txt is to avoid an "ambiguous redirect" error i got when running the script.

Update @Just Somebody

root@server:/home/user/Desktop# dmidecode | sed -n 's/Address://p'
         0xE0000
root@server:/home/user/Desktop#

Solution The use of |sed -n 's/^.*Address:.*0x/0x/p' got rid of the "? " in 0xE0000.txt

A big thanks to everyone!

Upvotes: 0

Views: 114

Answers (1)

pjz
pjz

Reputation: 43057

You've got a nonprinting char in there. Try:

dmidecode |grep -i Address|sed 's/Address://'| od -c

to see exactly what you're getting.

UPDATE: comments indicate there's a tab char in there that needs to be cleaned out.

UPDATE 2: the leading tab is before the word Address. Try:

name=$(dmidecode |grep -i Address|sed 's/^.*Address:.*0x/0x/')

or as @just_somebody points out:

name=$(dmidecode|sed -n 's/^.*Address:.*0x/0x/p')

UPDATE 3

This changes the substitution regex to replace

^ (start of line) followed by .* (any characters (including tab!)) followed by Address: followed by .* (any characters (including space!)) followed by 0x (which are always at the beginning of the address since it's in hex)

with

0x (because you want that as part of the result)

If you want to learn more, read about sed regular expressions and substitutions.

Upvotes: 1

Related Questions