Chopping strings Flashcards
1
Q
Basename
A
>basename /etc/sysconfig/iptables
iptables
2
Q
Dirname
A
>dirname /etc/sysconfig/iptables
/etc/sysconfig
3
Q
Chopping substrings - REMOVE longest from beginning with wild card
A
>MYVAR=foodforthought.jpg
>echo ${MYVAR##*fo}
rthought.jpg
- The { } are used to define the string in the variable MYVAR
- The 2 # indicate the fact we are looking for the longest string containing the wild card
- The string following the * specify the wild card string. in this case fo
- So we would like to display the portion of $MYVAR from which the longest string starting from the left and ending in fo is removed
4
Q
Chopping substrings - REMOVE shortest from beginning with wild card
A
>MYVAR=foodforthought.jpg
>echo ${MYVAR#*fo} odforthought.jpg
- The { } are used to define the string in the variable MYVAR
- The 1 # indicate the fact we are looking for the longest string containing the wild card
- The string following the * specify the wild card string. in this case fo
- So we would like to display the portion of $MYVAR from which the shortest string starting from the left and ending in fo is removed
5
Q
Using # versus %
A
truncates from beginning of string % truncates from end of string
6
Q
DISPLAY by specifying starting point and count numbers of characters to take out
A
>EXCLAIM=cowabunga
>echo ${EXCLAIM:0:3}
cow
- started at 0, the left of the string, and DISPLAY the first 3 chracaters
>echo ${EXCLAIM:3:7}
abunga
- start at the third characters and display seven following characters ALWAYS TEST THESE BEFORE USING