Linux Shell String Common operations

Source: Internet
Author: User

    1. Take the length of the variable

[[Email protected] scripts]# var=oldboy123[[email protected] scripts]# echo  ${var}oldboy123[[email protected] scripts]# echo ${#var}9# For example: #以下判断用read输入一个值, if the length is 0, The value is empty, otherwise the value of the variable is printed [[email protected] scripts]# cat if4.sh #!/bin/bashread -p  " pls input a num:  " aif [ ${#a} -eq 0 ]then      echo  "a is null,pls input a nums again!"     exit 1else    echo  "a= $a" fi# test [[email protected]  scripts]# sh if4.sh pls input a num: a is null,pls  input a nums again! [[email protected] scripts]# sh if4.sh pls input a num: da=d[[ Email protected] scripts]# sh if4.sh pls input a num: 123a=123



2. File name and directory interception

[Email protected] scripts]# test= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] scripts]# echo ${test }/etc/sysconfig/network-scripts/ifcfg-eth0# file name [[email protected] scripts]# echo ${test##*/}ifcfg-eth0# fetch directory [email Protected] scripts]# echo ${test%/*}/etc/sysconfig/network-scripts# use commands basename and dirname can also achieve the same effect [email protected ] scripts]# echo ' basename $test ' ifcfg-eth0[[email protected] scripts]# echo ' dir $test '/etc/sysconfig/network-script S/ifcfg-eth0


Linux Shell String Common operations

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.