Shell string Processing

Source: Internet
Author: User

Output the top 10 system user names and UID numbers of the current system to the screen
head/etc/passwd

String interception
1${variable name: Start position: Length}

echo ${variable name: Start position: Length}
Variable name =${variable name: Start position: Length}

806 Echo $HOSTNAME

808 ${hostname:0:3}
809 Echo ${hostname:0:3}
810 Echo $HOSTNAME
811 Echo ${hostname:0:5}
812 Domain=${hostname:0:5}
813 Echo $domain
814 Domain=${hostname:6}

2 Using expr substr
[Email protected] ~]# echo $HOSTNAME
Pc205.tarena.com
[Email protected] ~]#
[[email protected] ~]# expr substr "$HOSTNAME" 1 5
pc205
[[email protected] ~]# expr Length "$HOSTNAME"
16
[[email protected] ~]# expr Length "panglijing"
10
[Email protected] ~]#

3 Using the Cut tool
echo $var | Cut-b start position-end position
echo $HOSTNAME | Cut-b 1-5
echo $HOSTNAME | Cut-b 6-

Head-3/etc/passwd | Cut-d ":"-F 1,3

4 Path Segmentation
BaseName
DirName


[Email protected] ~]# Basename/etc/sysconfig/network-scripts/ifcfg-eth0
Ifcfg-eth0
[Email protected] ~]#
[Email protected] ~]#
[Email protected] ~]# BASENAME/ETC/PASSWD
passwd
[Email protected] ~]# basename/etc/sysconfig/network-scripts
Network-scripts
[Email protected] ~]# basename/etc/sysconfig/network-scripts
Network-scripts
[Email protected] ~]#

[Email protected] ~]# Dirname/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
[Email protected] ~]#
[Email protected] ~]# dirname/etc/sysconfig/network-scripts
/etc/sysconfig
[Email protected] ~]#

+++++++++++++++++++++++++++++++++++++++++++++++++

String substitution
${var/old/new}
${var//old/new}

Name=panglijing
[[Email protected] Desktop]# echo ${name/i/f}
Panglfjing
[[Email protected] Desktop]# echo ${name//i/f}
Panglfjfng
[Email protected] Desktop]# echo $name
Panglijing
[[Email protected] Desktop]# newname=${name//i/f}
[Email protected] Desktop]# echo $newname
Panglfjfng
[[Email protected] Desktop]#

by condition Qiatouquwei
${var Replace symbol expression}
echo ${var replace symbol expression}
Variable name =${var replace symbol expression}

How expressions are represented
Wildcard characters
Normal characters
Wildcard characters + normal character

String deletion
Left-to-right, delete the data that matches the expression in the variable
# Shortest Match Delete
# # longest Match Delete

Right-to-left, delete data that matches the expression in a variable
% Shortest Match Delete
Percent of the longest match delete

825 Echo ${mail##*/}
826 mailbox=${mail##*/}
827 mailuser=${mail##*/}
828 echo $mailuser
829 Echo $MAIL
830 echo ${mail%/root}
831 Echo ${mail%/root}
832 echo $MAIL
833 Echo ${mail%/*}
----------------------------------------------------------------------
Get partial data for a string
----------------------------------------------------------------------
Assigning an initial value to a variable
${variable name symbol "value"}
echo ${variable name symbol "value"}
Variable name =${variable name symbol "value"}

--------------------------------------------------------------------
:=
${var:= "word"}
If the variable var already exists and is not NULL, the $var value is returned.
Otherwise, return the string "word" and assign a value to the variable var

---------------------------------------------------------------------
:+
${var:+ "word"}
If the variable var already exists and is not NULL, the string "word" is returned and a null value is returned instead.
Role: Determine whether a definition.

----------------------------------------------------------------------
:-
${var:-"word"}
If the variable var already exists and is not NULL, the value returned $var
Otherwise, the string "word" is returned, and the variable var value does not change.
----------------------------------------------------------------------
:?
${var:? }
${var:? "Word"}
If the variable var already exists and is not NULL, the value returned $var
, otherwise returns the string "word" and ends the script.
Function: The error caused by the output dependent variable not being defined.

#!/bin/bash
Read-p "Please input numbers" x
#x =${x:-"1"}
#y =${x:= "1"}
#y =${x:+ "1"}
Y=${x:?}
Echo ' $x is ' $x
Echo $y
---------------------------------------------------------------------
[email protected] shell]# cat check_veryone_ip.sh
#!/bin/bash
ip=${1:-"127.0.0.1"}
Ping-c 3 $ip &>/dev/null
If [$?-eq 0];then
Echo $ip is online
Else
Echo $ip is not online
Fi
[Email protected] shell]#

This article is from the Linux Cloud Computing blog, so be sure to keep this source http://liangzai818.blog.51cto.com/10003446/1763437

Shell string Processing

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.