Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Redirection, Pipeline and echo commands in shell

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the shell redirection, pipeline and echo command example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Redirect, pipe, and echo commands in shell

We see that only the grep mysql process exists on the first lookup, and none on the second lookup. Grep-v grep is to delete the process we are looking for.

Let's talk about echo commands. As we all know, echo is used for printing. But some of its advanced operations, do you know? Next, let's talk about this mysterious echo.

Format: echo [options] [characters, variables.]

Options:-n output without line wrapping, echo default to output one line at a time;-e enhancement to enable transfer character parsing:\ a ring,\ b backspace,\ c remove trailing new line,\ nnew line,\ r carriage return,\ t horizontal tab,\ slash,\ 0NN octal,\ 0xHH hexadecimal output.

For example: echo-e "welcom to\ t new world\ nshell\\ programming", let's see the effect.

We see that the pointer appears at the first\ t, the newline character appears at\ nand\\ is the slash.

Come down and see how to output colored characters. Foreground color: 3 of ten digits, range: 30-37 (3x); background color: 4 of ten digits, range: 40-47 (4x); single digits represent colors: 30 (black), 31 (red), 32 (green), 33 (× ×), 34 (blue), 35 (magenta), 36 (cyan), 37 (white).

For example: echo-e "Network restarting... [\ 033 [32 countries 40m OK\ 033 [0m]", the effect is as follows

Let's take a look at a practical application of echo. Write a script: prompt the user for a password, encrypt the password with base64 or md5, and then save the encrypted password to the / home/aston/test/a.txt file.

Steps: 1, prompt for password; 2, read password; 3, encrypt password; 4, redirect input to file; 5, view password.

1. Prompt for password

#! / bin/bashecho "Please input a password:"

Let's see how it works.

Let's change its executable permissions first, and then execute it. The first step above has been achieved.

2. Read the password

#! / bin/bashecho "Please input a password:" read PASSecho "your passwd is: $PASS"

Let's see the results.

The function of reading password has been implemented.

3. Encrypt the password

#! / bin/bashecho "Please input a password:" read PASSPASS=$ (echo $PASS | openssl base64) echo "your passwd is: $PASS"

Let's see how it works.

We see that the password has been encrypted.

4, redirect input to the file; 5, view the password.

#! / bin/bashecho "Please input a password:" read PASSecho $PASS | openssl base64 > / home/aston/test/a.txt

Let's see how it works.

Then we certainly don't want to be seen when entering the password, so we have to do something about it.

! / bin/bashecho "Please input a password:" echo-n-e "\ 033 [30x 40m" read PASSecho-e "\ 033 [0m" echo $PASS | openssl base64 > / home/aston/test/a.txt

Let's set the background color to black to see the effect.

We see that when entering the password, the background is black and the password is still encrypted. At this point, we have learned the pipes and echo commands in shell, which will be more convenient in future development.

These are all the contents of the article "sample Analysis of redirects, pipes, and echo commands in shell". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report