Shell finishing (25) ===== replacing uppercase and lowercase letters

Source: Internet
Author: User
Tags lowercase

This is my Sina blog, with all the shells I wrote


I wrote a lot of my own practice in the Sina blog Shell, I am going to write in 51, above is a hyperlink.

(a) accept an input and replace the first letter of the input string.

For example

[Email protected] hushuai]# bash 1.sh

Input word:ding Xue

Ding Xue

[Email protected] hushuai]#


The shell code is as follows:

#!/bin/bash


>file1 #每执行一次脚本清空次这个文件

Read-p "Input word:" ll #read-P is used to accept an input

echo $ll |xargs-n1>file # Xargs The user's input into a column, the role of the

The use of cat file |while read line #read reads one row at a time and assigns the line

Do

B= ' echo $line |cut-c1 ' # Number of lines on the loop how many times, take the first letter in each line

D= ' echo $line |cut-c2-' # reads each line to intercept the 2nd subsequent letter, i.e. every read

C= ' echo $b |tr ' A-Z ' A-Z ' a line, loop once, we'll split this line into two parts

Echo-n "$c $d" >>file1 # Details There is a space behind the $c$d, otherwise the output of each row will

Done compactly together.

Cat File1

echo # Use-N in the loop, make sure to write an echo at the end

Or they're going to be linked together.

Shell's version 2

#!/bin/bash


>bb

[-Z] && echo "Usage: ' basename $ ' word ' && exit # ' basename $ ' print out the name of the script

a=$# #$# number of statistical parameters, separated by spaces

echo [email protected] |xargs-n1 >aa #$@ means all the parameters passed in

For i in ' seq $a '

Do

c= ' Cat AA | Sed-n ' $i ' P ' |cut-c1 ' # comes in several loops, middle and

d= ' Cat AA | Sed-n ' $i ' P ' |cut-c2-' above the same principle

E= ' echo $c |tr ' A-Z ' A-Z '

Echo-n "$e $d" >>bb

Done

Cat BB

Echo

The core of the two is the same, we can also use SED to complete

For example:

[Email protected]/]# echo ding Xue | Sed-r ' s/([^]+)/\u&/g '

Ding Xue

[Email protected]/]#





I have some summary of the knowledge Inside:

For example: Xargs on two functions, the line into a column, one column for a row


[[email protected]/]# SEQ 3 |xargs

1 2 3

[[email protected]/]# SEQ 3 |xargs-n2

1 2

3

[[email protected]/]# seq 3 |xargs-n2 |xargs

1 2 3

If it is an accepted user input we generally use the Read command

There are generally two ways to do this:

(1) read-p "Input:" A

Echo $a


(2) echo-n "Input:" # details, I write-n is reasonable, otherwise the output is the first kind

Read Input condition. Not beautiful.

Echo $Input

Output Result:

[Email protected]/]# bash 18.sh

Input:

A

A

[Email protected]/]# bash 18.sh

Input:a

A

~

READ also has the usual read-s does not echo Read-t waits for the user to enter the longest time

Several common parameters that may be encountered in a script

For example:


#!/bin/bash


a=$#

Echo $a $# number of parameters passed in the statistics

[e-Mail protected] $@ the string passed in

Echo $b

~

Look at the effect:

[Email protected]/]# bash 16.sh ding xue

2

Ding Xue

[Email protected]/]#

echo $b | Xargs-n1 If you add such a bar, the result will be:

[Email protected]/]# bash 16.sh ding xue

2

Ding Xue

Ding

Xue

[Email protected]/]#

If we replace the article with this:

C= ' echo $b | Xargs-n1 '

Echo $c

The output changes to:

[Email protected]/]# bash 16.sh ding xue

2

Ding Xue

Ding Xue # This place becomes a horizontal arrangement, because with the anti-quote Shell will think that the vertical

[[Email protected]/]# things cross over. For example:

[[email protected]/]# echo ' seq 9 '

1 2 3 4 5 6 7 8 9

[Email protected]/]#

When we are affected by the read input, we often use:

#!/bin/bash

Read-p "Input:" A

b=${#a} # Number of statistics strings (including spaces)

Echo $b

Look at the results

[Email protected]/]# bash 15.sh

input:ding Xue

8 # 8 Explanations of what, the space is counted.

[Email protected]/]#


This article from "It Life" blog, declined reprint!

Shell finishing (25) ===== replacing uppercase and lowercase letters

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.