"Linux command line and Shell Scripting Encyclopedia" chapter 26th some interesting scripts

Source: Internet
Author: User
Tags save file

26.1 Sending messages 26.1.1 function analysis

1. Determine who is in the system

$who

The information given includes the user name of the user at the end of the user log in the system time

2. Enable message feature

Users can prohibit people from sending messages to me, so you need to check to see if they are allowed to send messages.

$mesg

The result is an is n stating that the message was sent off. Is y means that messages are allowed to be sent

You can also view other people's message status,

$who –T

After the user name-indicates that the user's message function has been closed. + indicates already enabled

To accept a message, you can use the MESG y option so that the message function is enabled.

$MESG y

3. Send messages to other users

After the message feature is enabled, you can use the Write command to send a message to it via the user name and the current terminal

With WHO view: Send Message to Xiaochongyong PTS/19

$write Xiaochongyong Pts/19 #这样就是发送消息

I opened two terminals, one was PTS/17 and the other was 19. Here are the 19 received

Note: The receiver often needs to press ENTER to regain command prompt after receiving a message

26.1.2 Creating a script

There are several steps

1) First check whether the user is logged in

2) Check whether the user can receive messages

3) Check if the message you want to send is included

4) Send Message

The code is as follows:

[Email protected]:~/shell/26zhang$ Cat mu.sh

#!/bin/bash

Muser=$1

logged_on=$ (who | grep-i-M 1 $muser | gawk ' {print $} ')

#1) Determine if user is logged

If [-Z $logged _on]

Then

echo "$muser isn't logged on"

echo "Exiting script ..."

Exit

Fi

#2) Determine if user allows messages

allowed=$ (who-t | grep-i-M 1 $muser | gawk ' {print $} ')

if [$allowed! = "+"]

Then

echo "$muser does not allowing messageing."

echo "Exiting script ..."

Exit

Fi

# 3) Check message

If [-Z]

Then

echo "No message parameter included"

echo "Exiting script ..."

Exit

Fi

Shift

While [-N "$"]

Do

Whole_message= $whole _message "$

Shift

Done

# 4) Send message to User

uterminal=$ (who | grep-i-M 1 $muser | gawk ' {print $} ')

echo $whole _message | Write $logged _on $uterminal

Exit

Operation Result:

You can see a reminder in the lower right corner:

26.2 Get the maxim 26.2.1 function analysis

Extract a line of proverbs on the specified URL, inspirational words.

1. Learning wget

wget can download Web pages to a local Linux system.

The function is very rich, this is only a little bit.

How to use, use the wget command and the website address on the line

$wget www.baidu.com

The information for the Web site is stored in a file with the same name as the Web page. Here is saved in the index.html

You can also specify that session output is saved in a log file by using the-o option

$wget –o Baidu.log www.baidu.com

You can also use-O to control where Web page information is saved. You can specify the file name yourself,

$wget –o baidu.log–o baidu.html www.baidu.com

2. Test Web address

A write Wed address will change. So you need to test the validity of the address.

Using the –spider option

$wget –spider www.baidu.com

You can also add the-NV option to streamline the output information

$wget –SPIDER–NV www.baidu.com

You can test an invalid address here.

$wget –SPIDER–NV www.xiaochongyong.com

26.2.2 Creating a script

[Email protected]:~/shell/26zhang$ Cat quota.sh

#!/bin/bash

Quote_url=www.quotationspage.com/qotd.html

#quote_url =www.baidu.com

check_url=$ (wget-nv--spider $quote _url 2>&1)

if [[$check _url = = *error404*]]

Then

echo "Bad web address"

echo "$quote _url Invalid"

echo "Exiting script ..."

Exit

Fi

#wget-o/tmp/quote.log-o/tmp/quote.html $quote _url

Wget-o quote.log-o quote.html $quote _url

sed ' s/<[^>]*//g ' quote.html | # removal <>

#grep "$ (date +%b '%-d, '%Y) '-a2 | # matches the right side of the current date in the maxim. The-A2 option extracts Two additional lines of text

grep ", $ (date +%y)"-a2 | # Xcy Add

sed ' s/>//g ' |  # Delete >

Sed '/&nbsp;/{n; d} ' |

Gawk ' begin{fs= ' &nbsp; '} {print $} ' |

Tee Daily_quote.txt >/dev/null # Save File

Exit

[Email protected]:~/shell/26zhang$ ./quota.sh

[Email protected]:~/shell/26zhang$ Cat Daily_quote.txt

Selected from Michael Moncur ' s Collection of Quotations-november 28, 2017

Perpetual devotion to what a man calls his business, is the only-be-sustained by perpetual neglect of many and other things. Robert Louis Stevenson (1850-1894)

[Email protected]:~/shell/26zhang$

Note this:#grep "$ (date +%b '%-d, '%Y)"-A2 |

Since my Ubuntu is in Chinese, I have not been searching for November. I'm going to replace it with this one, only 28, 2017.

grep ", $ (date +%y)"-a2 | # Xcy Add

It would have been like this grep "$ (date +%-d, '%Y)"-A2 | # xcy Add, don't know why my date is 29. And the site is 28. May be related to the U.S. website.

26.3 Fabricating excuses

The Curl tool allows you to receive data from a specific Web server. You can also use it to send data.

The test here requires an SMS card from the United States and is not studied.

"Linux command line and Shell Scripting Encyclopedia" chapter 26th some interesting scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.