Unix & Linux Asked on November 21, 2021
I’m newbie in bash scripting
I want to format command with printf to prepare execution and assign into variable.
But it is executing immediately. It is listing /opt/cinar/packages/libcnrhttp2_*
instead of writing it as string into command string.
I was expecting this:
ssh [email protected] ls /opt/cinar/packages/libcnrhttp2_* -lr | awk 'NR==1'
#!/bin/bash
declare -r libcnrhttp2="libcnrhttp2_"
declare -r root="/opt/cinar/packages"
declare -r destination="/opt/cinar/packages"
declare -r id=ctopkaya
declare -r host=192.168.13.137
declare -r remote=$id"@"$host
declare cmd=""
printf -v cmd ssh %s ls %s/%s* -lr | awk 'NR==1' $remote /opt/cinar/packages libcnrhttp2_
cd $destination
echo "--------------"
echo $cmd
echo "--------------"
The command which is supposed to format the text is within double quotes and is thus treated as text to print. To print the result of a command instead put it inside $()
(command substitution), like:
printf "$(printf foo | awk 'NR==1') bar"
Answered by DisplayName on November 21, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP