Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
---|---|---|
Prev | Chapter 9. Variables Revisited | Next |
Bash�Ѿ�֧�������˾��ȵ��ַ������������������ҵأ���Щ����ȱ��ͳһ�ı���һЩ�������滻���Ӽ��������ܵ�UNIX��expr����Ĺ��ܵ�Ӱ�졣��²�һ�µ������������Ĺ��ܣ�����Щ��û��������ҡ�
�ַ�������
1 stringZ=abcABC123ABCabc 2 3 echo ${#stringZ} # 15 4 echo `expr length $stringZ` # 15 5 echo `expr "$stringZ" : '.*'` # 15 |
���� 9-10. ��һ���ı��ļ��Ķ���֮�����һ���հ���
1 #!/bin/bash 2 # paragraph-space.sh 3 4 # �������о���ı��ļ�����֮�����һ���հ���. 5 # Usage: $0 <FILENAME 6 7 MINLEN=45 # ������Ҫ�ı����ֵ. 8 # Assume lines shorter than $MINLEN characters 9 #+ terminate a paragraph. 10 11 while read line # �ṩ�������ļ�һ�������... 12 do 13 echo "$line" # ����б���. 14 15 len=${#line} 16 if [ "$len" -lt "$MINLEN" ] 17 then echo # ��һ�����н������ӡһ���հ���. 18 fi 19 done 20 21 exit 0 |
ƥ���ַ�����ͷ���Ӵ��ij���
$substring ��һ���������ʽ.
$substring ��һ���������ʽ.
1 stringZ=abcABC123ABCabc 2 # |------| 3 4 echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8 5 echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8 |
����
���ַ���$string��$substring��һ�γ��ֵ�����λ��
1 stringZ=abcABC123ABCabc 2 echo `expr index "$stringZ" C12` # 6 3 # C �ַ���λ��. 4 5 echo `expr index "$stringZ" 1c` # 3 6 # 'c' (in #3 position) matches before '1'. |
���C���Ժ���strchar()�dz����ơ�
��$string�дӵ�$postion���ַ���ʼ�ַ�����ȡ����.
���$string��"*"��"@"�����ʾ��λ�ò�������ȡ��$postion������ַ�����[1]
��$string��$postion���ַ�����ij���Ϊ$length���ַ�����ȡ������
1 stringZ=abcABC123ABCabc 2 # 0123456789..... 3 # ��0��ʼ����. 4 5 echo ${stringZ:0} # abcABC123ABCabc 6 echo ${stringZ:1} # bcABC123ABCabc 7 echo ${stringZ:7} # 23ABCabc 8 9 echo ${stringZ:7:3} # 23A 10 # ��ȡ���Ӵ���Ϊ3 11 12 13 14 # ��û�п��ܴ��ַ������ұ߽�β����ȡ? 15 16 echo ${stringZ:-4} # abcABC123ABCabc 17 # Ĭ���������ַ��������൱��${parameter:-default}. 18 # Ȼ��. . . 19 20 echo ${stringZ:(-4)} # Cabc 21 echo ${stringZ: -4} # Cabc 22 # ����,�����Թ�����. 23 # Բ���ŻӵĿհ��ַ�����ת��$position����. 24 25 # ��лDan Jacobsonָ�����. |
���$string������"*"��"@"�������ȡ��$length��λ�ò�����ʼ�Ĺ�$length��������[����ע��ʵ��ȡ�õIJ����п�������$length����Ϊ�п������µIJ���û����ô����]
1 echo ${*:2} # ��ӡ�ڶ���λ���Ժ�IJ���. 2 echo ${@:2} # ������һ��. 3 4 echo ${*:2:3} # ��ӡ�ӵڶ��������������λ�ò���. |
��ȡ$string�д�λ��$postition��ʼ�ij���Ϊ$length�����ַ�����
1 stringZ=abcABC123ABCabc 2 # 123456789...... 3 # ��1��ʼ����. 4 5 echo `expr substr $stringZ 1 2` # ab 6 echo `expr substr $stringZ 4 3` # ABC |
��$string�ַ�����߿�ʼ��ȡ��ȡ��$substring�������������ʽ���Ӵ���
��$string�ַ�����߿�ʼ��ȡ��$substring�������������ʽ���Ӵ���
1 stringZ=abcABC123ABCabc 2 # ======= 3 4 echo `expr match "$stringZ" '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1 5 echo `expr "$stringZ" : '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1 6 echo `expr "$stringZ" : '\(.......\)'` # abcABC1 7 # �����ÿ��echo����ӡ��ͬ�Ľ��. |
��$string�ַ�����β��ʼ��ȡ��$substring�������������ʽ���Ӵ���
��$string�ַ�����β��ʼ��ȡ��$substring�������������ʽ���Ӵ���
1 stringZ=abcABC123ABCabc 2 # ====== 3 4 echo `expr match "$stringZ" '.*\([A-C][A-C][A-C][a-c]*\)'` # ABCabc 5 echo `expr "$stringZ" : '.*\(......\)'` # ABCabc |
�Ӵ��ƶ�
��$string��߿�ʼ����ȥ���ƥ��$substring�Ӵ�.
��$string��߿�ʼ����ȥ�ƥ��$substring�Ӵ�.
1 stringZ=abcABC123ABCabc 2 # |----| 3 # |----------| 4 5 echo ${stringZ#a*C} # 123ABCabc 6 # ��ȥƥ��'a'��'C'֮����̵��ַ���. 7 8 echo ${stringZ##a*C} # abc 9 # ��ȥƥ��'a'��'C'֮������ַ���. |
��$string��β��ʼ����ȥ���ƥ��$substring�Ӵ���
��$string��β��ʼ����ȥ�ƥ��$substring�Ӵ���
1 stringZ=abcABC123ABCabc 2 # || 3 # |------------| 4 5 echo ${stringZ%b*c} # abcABC123ABCa 6 # ��$stringZ����β����ʼ����ȥƥ��'a'��'C'֮����̵��ַ���. 7 8 echo ${stringZ%%b*c} # a 9 # ��$stringZ����β����ʼ����ȥƥ��'a'��'C'֮������ַ���. |
���� 9-11. �����ļ����ĸ�����ת��ͼ���ļ��ĸ�ʽ
1 #!/bin/bash 2 # cvt.sh: 3 # ��һ��Ŀ¼�µ�����MacPaintͼ���ļ�ת����"pbm"��ʽ. 4 5 # ʹ��������"netpbm"�е�"macptopbm"������ת��, 6 #+ ���������Brian Henderson(bryanh@giraffe-data.com)ά��. 7 # Netpbm�Ǵ����Linux���а�ı���. 8 9 OPERATION=macptopbm 10 SUFFIX=pbm # �µ��ļ���. 11 12 if [ -n "$1" ] 13 then 14 directory=$1 # ���һ��Ŀ¼�����ݸ��ű�... 15 else 16 directory=$PWD # ����ʹ�õ�ǰĿ¼. 17 fi 18 19 # �ٶ���Ŀ��Ŀ¼�У� 20 #+ ���Ǵ���".mac"����MacPaintͼ���ļ�. 21 22 for file in $directory/* # �ļ���ƥ���. 23 do 24 filename=${file%.*c} # �����ļ����е�".mac"���� 25 #+ '.*c'ƥ������'.'��'c'֮�����е�ƥ���ַ� 26 27 $OPERATION $file > "$filename.$SUFFIX" 28 # �ѽ���ض����µ��ļ��� 29 rm -f $file # ת����ɾ��ԭ�����ļ�. 30 echo "$filename.$SUFFIX" # ��ӡһ�����ij�ļ�����Ϣ�������. 31 done 32 33 exit 0 34 35 # ��ϰ: 36 # -------- 37 # �������ڵ����,����ű�ת����Ŀ¼�����е��ļ� 38 # 39 # ������ʹ��ֻת����Ϊ".mac"���ļ�. |
һ����ʹ���Ӵ���ȡ�ṹ��getoptģ�¡�
���� 9-12. ģ��getopt
1 #!/bin/bash 2 # getopt-simple.sh 3 # ����: Chris Morgan 4 # ͬ����ABSָ����ʹ��. 5 6 7 getopt_simple() 8 { 9 echo "getopt_simple()" 10 echo "Parameters are '$*'" 11 until [ -z "$1" ] 12 do 13 echo "Processing parameter of: '$1'" 14 if [ ${1:0:1} = '/' ] 15 then 16 tmp=${1:1} # ��ȥǰ���ַ�'/' . . . 17 parameter=${tmp%%=*} # ��ȡ������. 18 value=${tmp##*=} # ��ȡ����ֵ. 19 echo "Parameter: '$parameter', value: '$value'" 20 eval $parameter=$value 21 fi 22 shift 23 done 24 } 25 26 # ������ѡ�������getopt_simple(). 27 getopt_simple $* 28 29 echo "test is '$test'" 30 echo "test2 is '$test2'" 31 32 exit 0 33 34 --- 35 36 sh getopt_example.sh /test=value1 /test2=value2 37 38 Parameters are '/test=value1 /test2=value2' 39 Processing parameter of: '/test=value1' 40 Parameter: 'test', value: 'value1' 41 Processing parameter of: '/test2=value2' 42 Parameter: 'test2', value: 'value2' 43 test is 'value1' 44 test2 is 'value2' |
�Ӵ��滻
��$replacement�滻��$substringƥ����ַ�����
��$replacement�滻����ƥ��$substring���ַ�����
1 stringZ=abcABC123ABCabc 2 3 echo ${stringZ/abc/xyz} # xyzABC123ABCabc 4 #��'xyz'�����һ��ƥ���'abc'. 5 6 echo ${stringZ//abc/xyz} # xyzABC123ABCxyz 7 # ��'xyz'�������е�'abc'. |
���$string�ַ�������ǰ��ƥ��$substring�ַ�������$replacement�滻$substring.
���$string�ַ���������ƥ��$substring�ַ�������$replacement�滻$substring.
1 stringZ=abcABC123ABCabc 2 3 echo ${stringZ/#abc/XYZ} # XYZABC123ABCabc 4 # ��'XYZ'�滻ǰ�˵�'abc'. 5 6 echo ${stringZ/%abc/XYZ} # abcABC123ABCXYZ 7 # ��'XYZ'�滻��˵�'abc'. |
Bash�ű����Ե���awk���ַ��������������������Լ��ڽ����ַ���������.
���� 9-13. ��ȡ�ַ�������һ�ְ취
1 #!/bin/bash 2 # substring-extraction.sh 3 4 String=23skidoo1 5 # 012345678 Bash 6 # 123456789 awk 7 # ע��������������������IJ�ͬ����: 8 # Bash���ַ����ĵ�һ���ַ��ı�ų�Ϊ'0'. 9 # Awk���ַ����ĵ�һ���ַ��ı�ų�Ϊ'1'. 10 11 echo ${String:2:4} # position 3 (0-1-2), 4 characters long 12 # skid 13 14 # ��awk����Bash��${string:pos:length}��ͬ����substr(string,pos,length). 15 echo | awk ' 16 { print substr("'"${String}"'",3,4) # skid 17 } 18 ' 19 # ��һ���յ�"echo"�ɹܵ���һ���յ������awk, 20 #+ �����Ͳ����ṩһ���ļ�����awk. 21 22 exit 0 |
[1] | ���Ҫô���������в�����Ҫô���������IJ�����. |