Skip to content

Instantly share code, notes, and snippets.

@lancelothk
Last active August 29, 2015 14:05
Show Gist options
  • Save lancelothk/a2a9a11f8df503d2a893 to your computer and use it in GitHub Desktop.
Save lancelothk/a2a9a11f8df503d2a893 to your computer and use it in GitHub Desktop.
Example of shell script – string extraction:
copy from http://www.ibm.com/developerworks/aix/library/au-unixtext/
Example of shell script – string extraction:
$ cat string_example1.sh
#!/bin/sh
FILEPATH=/home/w/wyoes/samples/ksh_samples-v1.0.ksh
echo '${FILEPATH} =' ${FILEPATH} " # the full filepath"
echo '${#FILEPATH} =' ${#FILEPATH} " # length of the string"
echo '${FILEPATH%.*} =' ${FILEPATH%.*} " # truncate right of the last dot"
echo '${FILEPATH%%.*} =' ${FILEPATH%%.*} " # truncate right of the first dot"
echo '${FILEPATH%%/w*} =' ${FILEPATH%%/w*} " # truncate right of the first /w"
echo '${FILEPATH#/*/*/} =' ${FILEPATH#/*/*/} " # truncate left of the third slash"
echo '${FILEPATH##/*/} =' ${FILEPATH##/*/} " # truncate left of the last slash"
$ ./string_example1.sh
${FILEPATH}=/home/w/wyoes/samples/ksh_samples-v1.0.ksh # the full filepath
${#FILEPATH} = 42 # length of the string
${FILEPATH%.*}=/home/w/wyoes/samples/ksh_samples-v1.0 # truncate right of the last dot
${FILEPATH%%.*}=/home/w/wyoes/samples/ksh_samples-v1 # truncate right of the first dot
${FILEPATH%%/w*}=/home # truncate right of the first /w
${FILEPATH#/*/*/}=wyoes/samples/ksh_samples-v1.0.ksh # truncate left of the third slash
${FILEPATH##/*/}=ksh_samples-v1.0.ksh # truncate left of the last slash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment