Score:2

Why is printf command in shell script giving errors

cn flag

I have a script that sends me some info about different machines, and it's working fine in Fedora machines, but Ubuntu 20.04 gives me error for only one printf. I can't seem to see what am I doing wrong. So the statement is:

#printf "Last logins (last -10) \n `last -10` \n"
Last logins (last -10) 
bash: printf: ` ': invalid format character
 vecn1    pts/8        tmux(1233684).

whereas similar statements works fine:

printf "Default gw (ip ro ls) \n`ip ro ls` \n"
Default gw (ip ro ls) 
default via 192.168.111.1 dev en0 proto dhcp src 192.168.111.6 metric 100 
192.168.111.0/24 dev en0 proto kernel scope link src 192.168.111.6 
192.168.111.1 dev en0 proto dhcp scope link src 192.168.111.6 metric 100 

What am I doing wrong? Maybe those "%" numbers? Not sure why are there. Normally is just a hostname (ip). If I do just last -10 following is the output

vecn1    pts/8        tmux(1233684).%3 Mon Dec 26 11:29 - 12:01  (00:31)
vecn1    pts/7        tmux(1233684).%2 Mon Dec 26 11:29 - 12:01  (00:31)
vecn1    pts/6        tmux(1233684).%1 Mon Dec 26 11:29 - 12:01  (00:31)
vecn1    pts/5        tmux(1233684).%0 Mon Dec 26 11:29 - 11:29  (00:00)
vecn1    pts/8        tmux(1232989).%3 Mon Dec 26 11:27 - 11:29  (00:01)
vecn1    pts/7        tmux(1232989).%2 Mon Dec 26 11:27 - 11:29  (00:01)
vecn1    pts/6        tmux(1232989).%1 Mon Dec 26 11:27 - 11:29  (00:01)
vecn1    pts/5        tmux(1232989).%0 Mon Dec 26 11:27 - 11:27  (00:00)
vecn1    pts/8        tmux(1198190).%3 Mon Dec 26 11:03 - 11:04  (00:00)
vecn1    pts/7        tmux(1198190).%2 Mon Dec 26 11:03 - 11:04  (00:00)

wtmp begins Wed Jun 22 17:27:34 2022

Thanks

Score:2
hr flag

Yes it's likely the % symbols.

You should always avoid injecting code into the format string of the printf function; instead pass the result of the command as a argument using the appropriate format specifier (%s in the case of a string):

printf 'Last logins (last -10)\n%s\n' "`last -10`"

or better (avoiding the deprecated backtick form of command substitution)

printf 'Last logins (last -10)\n%s\n' "$(last -10)"
DenisZ avatar
cn flag
Yep that works, Thanks. I'm not sure if I understand how exactly this (%s) works, as I've read printf manual, but didn't really get it. I also need to eliminate backticks from my scripts, you are right.
DenisZ avatar
cn flag
btw, how would I go about injecting code 2 times like: `printf "Uptime $(uptime) \nVersion $(uname -a) \n"`
hr flag
@DenisZ ou can pass as many arguments as there are format specifiers - so `printf 'Uptime %s\nVersion %s\n' "$(uptime)" "$(uname -a)"`
DenisZ avatar
cn flag
I see, wonderful, that seems easy.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.