Hello,
'echo' command is not printing the exacting content as I needed.
I'm trying below, which is not echoing the correct content.
echo "XYZ\\`hostname`-volume" >> 123
# cat 123
XYZ
Where, It suppose to get
# cat 123
XYZ\hostname-volume
Please let me know how to resolve.
Thank you.
Yoda
February 20, 2013, 10:48pm
2
It works for me on HP-UX, Linux & SunOS
echo "XYZ\\`hostname`-volume"
XYZ\mycomputer-volume
OR
echo "XYZ\\$(hostname)-volume"
XYZ\mycomputer-volume
What is your OS
& SHELL
?
It work for me as well. Your syntax is fine. Can you check what output you are getting after typing
hostname
.
Yes, it works fine for most of my OS and shells.
Is there any command that we have to put above the echo command so that it will take particular shell?
This will only fail on few things, wondering how to fix that?
Yoda
February 21, 2013, 12:28pm
5
Put a Shebang in the beginning of your script.
E.g.
#!/bin/bash
I have # /usr/bin/ksh on the top of my script.
Is there any other way to solve this problem? Like using 'set'??
Yoda
February 21, 2013, 12:33pm
7
Just put below line on top of your script:
#!/usr/bin/ksh
I'm sorry the below line is there on my script.
#! /usr/bin/ksh
Wondering why it cant execute, on all the servers?
I know the syntax is correct, but how to make it work on all the servers?
Is ksh the same place on all servers, or can be made to seem so with a symlink? (The #! thing and similar concerns like execute permissions are detailed in the execvp() man page Man Page for execvp (opensolaris Section 0) - The UNIX and Linux Forums )
If the permissions or ksh location cannot be fixed, call it with "ksh script-name script-args".
/usr/bin/ksh
(Korn shell) is typically not available on BSD Unix and Linux, unless it is installed as an extra package.
Even hostname
might not exist an some SysV Unix.
But all Unix have /bin/sh
and should have uname -n
#!/bin/sh
echo "XYZ\\`uname -n`-volume" >> 123
If your script has got ksh extensions so does not run with /bin/sh
, try the following shebang:
#!/bin/ksh
or
#!/usr/bin/env ksh
Yes, I really liked the idea of
'uname -n'
But #! /usr/bin/ksh or other shebang is not the issue in here, because all the remaining 200 lines script works fine except below.
'echo "XYZ\\`uname -n`-volume" >> 123'
Which clearly says that it is not an issue concerned with shebang.
Please suggest me further. I believe is concerned with shell. If may be, how to fix this bug with respect any server?
What does it do wrong? Maybe s/b:
echo 'XYZ\'`uname -n`'-volume' >> 123
There is no problem with the syntax, as per I understand.
Because i used it over 100 servers and it worked perfectly alright.
echo "XYZ\\`uname -n`-volume" >> 123
cat 123
XYZ\hostname-login
But on some servers even thought they are same OS it is not working as desired.
echo "XYZ\\`uname -n`-volume" >> 123
cat 123
XYZ
Is there any command on shell scripting which can fix the SHELL issue?
Like set -o?? just wondering.
Call it with "sh scriptname'" so the actual location of 'sh' is determined by local $PATH environment.
ksh has a funny behavior with e.g.
echo "\\a"
I did not find an explanation in man ksh
,
but it helped to replace echo
with printf...
:
printf "%s\n" "\\a"
In echo \a is a meta character, even if echo is a bash built in (mimics the exec()-able command /bin/echo): Man Page for echo (opensolaris Section 1) - The UNIX and Linux Forums
echo '\01' is cntrl-a cntrl-j is 0x010A.
I found it now in man echo
on Solaris.
It describes /usr/bin/echo that is widely identical with the echo in ksh.
So the problem is the first character of `hostname` following a \
is treated specially.
The solution really is
printf "%s\n" "XYZ\\`uname -n`-volume" >> 123
1 Like
But if you are escaping one, you need to escape both.
madeingermany:
ksh has a funny behavior with e.g.
echo "\\a"
I did not find an explanation in man ksh
,
but it helped to replace echo
with printf...
:
printf "%s\n" "\\a"
We've gone over this several times before in other threads. The echo command is not portable if the 1st argument starts with a minus sign or if any argument contains a backslash character.
This isn't ksh versus bash or other shells; it is whether the echo on your system behaves as specified by UNIX System V, as specified by BSD, or as specified by Linux. All three behave differently when parsing options and when handling backslash characters.
If you are trying to write a portable script use printf instead of echo whenever it is possible that the arguments you would be passing to echo violate either of these constraints.
1 Like
You could just type in echo 'cntrl-v cntrl-a', a quoted real SOH character.