Fixed the output for file search in search-jar.sh
[smart-shell-script.git] / search-jar.sh
blob40b8df6c9e30bd12cf61864d5bf5db406cb0e02f
1 #!/bin/sh
2 echo Params: "$#" - \("$1"\) \("$2"\) \("$3"\) \("$4"\)
3 if test "$#" -ne 4
4 then
5 echo "Command takes exactly 4 params - Jar_name_pattern search_location file_name_pattern search_string
6 Use - for Jar_name_pattern to search for within all jars.
7 Use - for search_file_name to find with file_name_pattern only and avoid content search"
8 exit 1
9 fi
11 if test "$1" = "-"
12 then
13 jar_param="*.jar"
14 else
15 jar_param="$1"
17 if test "$4" = "-"
18 then
19 search_content=1
20 else
21 search_content=0
23 echo Executing: find "$2" -name "$jar_param"
24 file_list=`find "$2" -name "$jar_param"`
25 current_dir=`pwd`
26 cd /tmp/
27 mkdir search_file || rw_not=1
28 if test -n "$rw_not"
29 then
30 echo "/tmp/ is not writeable by current user thus can search file content and exiting"
31 exit 1
33 cd search_file
34 echo "Search Result:"
35 for jar_file in $file_list
37 inner_file_list=`jar tf "$jar_file" | grep "$3"`
38 if test "$search_content" = "0"
39 then
40 if test -n "$inner_file_list"
41 then
42 jar xf "$jar_file" $inner_file_list
43 for inner_file in $inner_file_list
45 has_content=`cat $inner_file | grep "$4"`
46 if test -n "$has_content"
47 then
48 echo Found in "$inner_file" @ "$jar_file"
50 done
51 rm -rf *
53 else
54 test -n "$inner_file_list" && echo "$jar_file"
56 done
57 cd ..
58 rm -rf ./search_file
59 cd "$current_dir"