Disabling echo service in Linux - linux

Please, explain me what is echo service in Linux and how can I disable it?
For example, now I can type "echo hello" and it will print in the terminal "hello".
Thanks.

echo is a terminal command that simply prints its arguments to the console, as you observe.
man is another command which gives instructions on usage of a command. It stands for 'manual'. Therefore, to learn more about the echo command, you can run man echo.
It is not recommended to remove the echo command from your system, since it is part of GNU Coreutils, which is an essential part of the system. If you remove it, there is a high possiblity you may break your system.
However, you may remove it with:
$ su
# rm /bin/echo
The su command will prompt for the administrator password, and if correctly given, will put you in root. This is required because 'normal' users do not usually have permission to remove essential system components.
You can exit root with the exit command.

echo is a command used to print text to stdout. Normally this just means writing to your terminal as you mention:
$ echo 'Hello world'
Hello world
For more information on this command, you can read the manpage. Here is the output from my machine:
$ man echo
NAME
echo - echo - display a line of text
echo - display a line of text
Synopsis
echo [STRING]
Description
echo displays a string of text.
The following options are available:
· -n, Do not output a newline
· -s, Do not separate arguments with spaces
· -E, Disable interpretation of backslash escapes (default)
· -e, Enable interpretation of backslash escapes
· -h, --help Display this help
Escape Sequences
If -e is used, the following sequences are recognized:
· \\ backslash
· \a alert (BEL)
· \b backspace
· \c produce no further output
· \e escape
· \f form feed
· \n new line
· \r carriage return
· \t horizontal tab
· \v vertical tab
· \0NNN byte with octal value NNN (1 to 3 digits)
· \xHH byte with hexadecimal value HH (1 to 2 digits)
Example
echo 'Hello World' Print hello world to stdout
echo -e 'Top\nBottom' Print Top and Bottom on separate lines, using an escape sequence
As far as disabling it, I can't think of any good reason to do that. As was mentioned in the comments, you could delete or move the binary so that the command cannot be called, but that just seems to be a bad bad bad idea, so I won't even mention how to do that here.

Related

Linux .sh auto set local variables empty when echo

./run.sh:
1. cp=warmonger-1.0.0.jar
2. cmmd="java -server -D64 -Xms200m -Xmx200m
-Dlog4j.configurationFile=$WARMONGER_HOME/etc/log4j2.xml
-classpath $cp warmonger.agent.WarmongerAgentApp"
3. echo $cmmd
execute results:
dataq.agent.DataqAgentApp -Xmx200m
-Dlog4j.configurationFile=/warmonger/etc/log4j2.xml
-classpath warmonger-1.0.0.jar
"warmonger.agent.WarmongerAgentApp" not appear.
I means if remove echo, java will be throw an exception: Couldn't find main class
You won't see $cp when you echo $cmmd, because the shell substitutes the value of the cp parameter (warmonger-1.0.0.jar) in the assignment to cmmd.
You can escape the dollar sign, or use single quotes if you don't want the shell to expand the parameter.
Your shell script is CR-LF terminated (DOS/Windows ends of lines). Thus, from bash point of view, the cp variable contains warmonger-1.0.0.jar<CR> (notice the trailing <CR>).
When you echo the content of the cp variable, <CR> is echoed too which puts the cursor at the beginning of the line (CR = carriage return). echo then prints the remaining of the arguments at the beginning of the line.
You can see it in your output:
"java -server -D64 -Xms200m -Xmx200m" is overwritten with "warmonger.agent.WarmongerAgentApp"
which, in turn, is overwritten by some other command output ("dataq.agent.Dataq")
Solution: turn your DOS/Windows text file into a UNIX one. See this answer.

What does "cat -A" command option mean in Unix

I'm currently working on a Unix box and came across this post which I found helpful, in order to learn about cat command in Unix. At the bottom of the page found this line saying: -A = Equivalent to -vET
As I'm new into Unix, I'm unaware of what does this mean actually? For example lets say I've created a file called new using cat and then apply this command to the file:
cat -A new, I tried this command but an error message comes up saying it's and illegal option.
To cut short, wanted to know what does cat -A really mean and how does it effect when I apply it to a file. Any help would be appreciated.
It means show ALL.
Basically its a combination of -vET
E : It will display '$' at the end of every line.
T : It will display tab character as ^I
v : It will use ^ and M-notation
^ and M-notation:
(Display control characters except for LFD(LineFeed or NewLine) and TAB using '^' notation and precede characters that have the high bit set with
'M-') M- notation is a way to display high-bit characters as low bit ones by preceding them with M-
You should read about little-endian and big-endian if you like to know more about M notation.
For example:
!http://i.imgur.com/0DGET5k.png?1
Check your manual page as below and it will list all options avaialable with your command and check is there -A present, if it is not present it is an illegal option.
man cat
It displays non-printing characters
In Mac OS you need to use -e flag and
-e Display non-printing characters (see the -v option), and display a dollar sign (`$') at the end of each line.

Printing a "Hello World" in bourne shell without directly using white space char

I'm currently trying to solve a tricky/silly challenge and i've come to a dead end.
The challenge is basically to form a one-liner /bin/sh compatible command line
which essentially outputs "Hello World" without directly typing White space or Tab characters in the command itself.
for example something like -
echo Hello World
would be invalid since we used white space twice in the command line.
Any ideas?
Assuming that IFS by default is set to space:
# echo${IFS}a${IFS}b
a b
Tested on Solaris 10 sh.
Cheating a little, but it gives the correct effect (superficially) in bash:
PS1=hello$'\x20'world$'\n'"$PS1"
for example,
$ PS1=hello$'\x20'world$'\n'"$PS1"
hello world
$
The problem is that it will print hello world after every command in future :-)

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!
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.

Is it possible to make stdout and stderr output be of different colors in XTerm or Konsole?

Is it even achievable?
I would like the output from a command’s stderr to be rendered in a different color than stdout (for example, in red).
I need such a modification to work with the Bash shell in the Konsole, XTerm, or GNOME Terminal terminal emulators on Linux.
Here's a solution that combines some of the good ideas already presented.
Create a function in a bash script:
color() ( set -o pipefail; "$#" 2>&1>&3 | sed $'s,.*,\e[31m&\e[m,' >&2 ) 3>&1
Use it like this:
$ color command -program -args
It will show the command's stderr in red.
Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.
color()... — Creates a bash function called color.
set -o pipefail — This is a shell option that preserves the error return code of a command whose output is piped into another command. This is done in a subshell, which is created by the parentheses, so as not to change the pipefail option in the outer shell.
"$#" — Executes the arguments to the function as a new command. "$#" is equivalent to "$1" "$2" ...
2>&1 — Redirects the stderr of the command to stdout so that it becomes sed's stdin.
>&3 — Shorthand for 1>&3, this redirects stdout to a new temporary file descriptor 3. 3 gets routed back into stdout later.
sed ... — Because of the redirects above, sed's stdin is the stderr of the executed command. Its function is to surround each line with color codes.
$'...' A bash construct that causes it to understand backslash-escaped characters
.* — Matches the entire line.
\e[31m — The ANSI escape sequence that causes the following characters to be red
& — The sed replace character that expands to the entire matched string (the entire line in this case).
\e[m — The ANSI escape sequence that resets the color.
>&2 — Shorthand for 1>&2, this redirects sed's stdout to stderr.
3>&1 — Redirects the temporary file descriptor 3 back into stdout.
Here's an extension of the same concept that also makes STDOUT green:
function stdred() (
set -o pipefail;
(
"$#" 2>&1>&3 |
sed $'s,.*,\e[31m&\e[m,' >&2
) 3>&1 |
sed $'s,.*,\e[32m&\e[m,'
)
You can also check out stderred: https://github.com/sickill/stderred
I can't see that there is any way for the terminal emulator to do this.
The interface between the terminal emulator and the shell/app is via a pseudo-tty, where the terminal emulator is on the master side and the shell/app on the other. The shell/app have both stdout and stderr connected to the same pty, so when the terminal emulator reads from the pty for the shell/app output it can no longer tell which was written to stdout and which to stderr.
You will have to use one of the solutions that intercepts the data between the application and the slave-pty and inserts escape codes to control the terminal output colo(u)r.
Here is a little Awk script that will print everything you pass it in red.
#!/usr/bin/awk -f
{ printf("%c[%dm%s%c[0m\n", 0x1B, 31, $0, 0x1B); fflush() }
It simply prints each line it receives on stdin within the necessary escape codes to display it in red. It is followed by an escape code to reset the terminal.
(If you need a different color, change the second argument in the above printf call from 31 to the number corresponding to the desired color.)
Save it to colr.awk, do a chmod a+x, and use it like so:
$ my_program | ./colr.awk
It has the drawback that lines may not be displayed in order, because stderr goes directly to the console, while stdout is piped through an additional process.
A simple solution to color stdout in red is to pipe it through grep:
program | grep .
This should not require installing anything, as grep should be already installed everywhere.
Taken from Dennis’s comment on superuser.com.
I think you should use the standard escape sequences on stderr. Have a look at this.
Hilite will do this. It's a lightweight solution, but you have to invoke it for each command, eg. hilite gcc myprog.c. A more radical approach is built in to my experimental shell Gush which shows stderr from all commands run in red, stdout in black. Either way is very useful for software builds where you have lots of output with a few error messages that could easily be missed if not highlighted.

Resources