Sunday, June 8, 2014

Unix Shell Example: Path Searching(2)

terminal:
1) -h fell into "-help" in the first while loop of script_1, script will output the usage and exit
2) -version  fell into -version block in the first while loop of script_1, script will output the version and exit
3) -help-me-out fell into "-*" block in the first while loop of script_1, script will output "unrecognized option" and then exit
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -h  
 script [-all] [-?] [-help] [-version] envvar pattern(s)  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -version  
 script_1 has version: 1.0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -help-me-out  
 Unrecognized option: -help-me-out  
 script [-all] [-?] [-help] [-version] envvar pattern(s)  

=====================================

1) ./script_1 is called without any option, then in the first loop processing options, it will just skip this part. Then at the second part processing "envvar" and expanding into "dirpath", envvar is taken as the default "simplest PATH" variable defined in the beginning. Finally, at the third part for checking the "envvar" and "dirpath", it fell into the block "$# -eq 0", at this time, script think that user doesn't have any file to process, and exit.
2) envvar is assigned with string "NOPATH", when script is trying to expand it into dirpath, ${NOPATH} is taken as empty. So the check "test -z '$dirpath'" is true. Script output the error information and then exit
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 NOPATH foo  
 empty directory search path  
 script [-all] [-?] [-help] [-version] envvar pattern(s)  

=====================================

1) -a tells the script that we want to look for all occurances of "foobar" at all places specified by "PATH" variable. In this case, envvar is assigned with "PATH", and dirpath will get the value from old "$PATH" environment variable. Then in the final loop, script will search file pattern foobar in each directory from $PATH
2) very similar from above explanation. The only difference is: we provide a name with space. By using double quote, we tell script that "name spaces" is a single name.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH foobar  
 foobar not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH "name spaces"  
 name spaces not found  

======================================

1) Explanation is similar as above. In the final loop, script will search "ls" from each directory, and finally it found that occurance, and output to terminal
2) $? means exit status of last run command, in this case, ls is found, return code is 0, meaning success
3) Script will search "?sh" from each directory of dirpath(PATH), and -a option tells script that, we want to find all occurances of this pattern. And in the end, two occurances are found and get output to terminal.
4) exit code is still 0 here, since all file patterns are found.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH ls  
 /bin/ls  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH '?sh'  
 /usr/bin/rsh  
 /usr/bin/ssh  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 0  

=======================================

1) The script will search "c89" "c99" "cc" "c++" "CC" "gcc" "g++", each pattern in directory of PATH. Output the file path to terminal if it is found, otherwise output error information. In this example, "CC" is not found in any variable of PATH
2) Since there is one file not found, EXIT_CODE get increased by one, and the final exit code is 1
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH c89 c99 cc c++ CC gcc g++  
 /usr/bin/c89  
 /usr/bin/c99  
 /usr/bin/cc  
 /usr/bin/c++  
 CC not found  
 /usr/bin/gcc  
 /usr/bin/g++  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 1  

========================================

awk script will generate a list like "file1 file2 file3......file20". And all these files are not found in PATH. And final exit code is 20, because none of files get outputted.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH $(awk 'BEGIN { while (n<20) printf("file%d ", ++n) }')  
 file1 not found  
 file2 not found  
 file3 not found  
 file4 not found  
 file5 not found  
 file6 not found  
 file7 not found  
 file8 not found  
 file9 not found  
 file10 not found  
 file11 not found  
 file12 not found  
 file13 not found  
 file14 not found  
 file15 not found  
 file16 not found  
 file17 not found  
 file18 not found  
 file19 not found  
 file20 not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 20  

=========================================

Following command is almost same as above command, the only difference is: script is looking for 200 files following the pattern, instead of just 20.
In the end, since script forced EXIT_CODE to be within 125, so the final exit code is 125.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a PATH $(awk 'BEGIN { while (n<200) printf("file%d ", ++n) }')  
 file1 not found  
 file2 not found  
 file3 not found  
 file4 not found  
 file5 not found  
 file6 not found  
 ......  
 file200 not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 125  

No comments:

Post a Comment