{"id":1423,"date":"2010-01-14T17:57:01","date_gmt":"2010-01-14T22:57:01","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1423"},"modified":"2016-03-04T10:18:21","modified_gmt":"2016-03-04T15:18:21","slug":"bash-samples","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1423","title":{"rendered":"Bash Samples"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Timestamp\">Timestamp<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Find_with_Permission_Denied_Filtered_Out\">Find with Permission Denied Filtered Out<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Find_String_in_Type_of_Files\">Find String in Type of Files<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Find_a_particular_class_file_in_a_directory_of_jar_files\">Find a particular class file in a directory of jar files<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Find_a_particular_class_file_in_all_jar_files\">Find a particular class file in all jar files<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Use_getopts\">Use getopts<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#grep_Examples\">grep Examples<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Remove_Old_Log_Files\">Remove Old Log Files<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Some_Debug_Methods\">Some Debug Methods<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<p>* See <a href=\"?p=487\">this post<\/a> for more examples.<\/p>\n<span id=\"Timestamp\"><h2>Timestamp<\/h2><\/span>\n<pre lang=\"bash\">\r\nts=`date +%m%d%Y`\r\nts2=`date +%H%M%S_%m%d%Y`\r\n<\/pre>\n<span id=\"Find_with_Permission_Denied_Filtered_Out\"><h2>Find with Permission Denied Filtered Out<\/h2><\/span>\n<pre lang=\"bash\">\r\nfind . -name \"something\" -print 2>\/dev\/null > find.something.file\r\n<\/pre>\n<span id=\"Find_String_in_Type_of_Files\"><h2>Find String in Type of Files<\/h2><\/span>\n<pre lang=\"bash\">\r\n#!\/bin\/sh\r\nif [ $# -lt 2 ]; then\r\n  echo \"Usage: findstr string_to_find type_of_file\"\r\n  exit 1\r\nfi\r\n\r\nstr2find=$1\r\ntype2find=$2\r\ndir2find=\".\"\r\nif [ $3 ];then\r\n  dir2find=$3\r\nfi\r\n\r\necho \"Finding string \\\"${str2find}\\\" in files ending with \\\"${type2find}\\\" in \\\"${dir2find}\\\" directory...\"\r\nfor f in `find ${dir2find} -name \"*.${type2find}\"`\r\ndo\r\n if [ ${type2find} = \"jar\" -o ${type2find} = \"war\" -o ${type2find} = \"zip\" ]; then\r\n   jar tvf $f | grep -i ${str2find}\r\n   if [ $? = '0' ]; then\r\n     echo \"Found ${str2find} in $f file.\"\r\n   fi\r\n else\r\n  grep -i ${str2find} $f\r\n  if [ $? = '0' ]\r\n  then\r\n    echo \"Found ${str2find} in $f file.\"\r\n  fi\r\n fi\r\ndone\r\necho \"done.\"\r\n<\/pre>\n<p>* Example:<\/p>\n<pre lang=\"bash\">\r\n]$ .\/findstr test jar\r\nFinding string \"test\" in files ending with \"jar\" in \".\" directory...\r\n    12 Wed Jun 04 12:01:10 EDT 2014 test.txt\r\nFound test in .\/test.jar file.\r\ndone.\r\n<\/pre>\n<span id=\"Find_a_particular_class_file_in_a_directory_of_jar_files\"><h2>Find a particular class file in a directory of jar files<\/h2><\/span>\n<pre lang=\"bash\">\r\n#!\/bin\/sh\r\nfindclass='ClassFileName'\r\nfor f in `ls`\r\ndo\r\n  jar tvf $f | grep ${findclass}\r\n  if [ $? = '0' ]\r\n  then\r\n    echo \"Found ${findclass} in $f file.\"\r\n  fi\r\ndone\r\n<\/pre>\n<span id=\"Find_a_particular_class_file_in_all_jar_files\"><h2>Find a particular class file in all jar files<\/h2><\/span>\n<pre lang=\"bash\">\r\n#!\/bin\/sh\r\n\r\nif [ $# -lt 2 ]\r\nthen\r\n  echo \"Usage: findclass fileNameToFind dirToStartSearch\"\r\n  exit 1\r\nfi\r\n\r\nfindclass=$1\r\nfinddir=$2\r\nfor f in `find ${finddir} -name \"*.jar\"`\r\ndo\r\njar tvf $f | grep ${findclass}\r\nif [ $? = '0' ]\r\nthen\r\necho \"Found ${findclass} in $f file.\"\r\nfi\r\ndone\r\n<\/pre>\n<p>* Usage example:<\/p>\n<pre lang=\"bash\">\r\nfindclass 'MyClassName' '\/app'\r\n<\/pre>\n<span id=\"Use_getopts\"><h2>Use getopts<\/h2><\/span>\n<pre lang=\"bash\">\r\n#--------------------------------------------\r\n# Get options\r\n#--------------------------------------------\r\nwhile getopts \":a:b:r:\" opt\r\ndo\r\n  case $opt in\r\n    a)\r\n      alpha=$OPTARG\r\n      ;;\r\n    b)\r\n      beta=$OPTARG\r\n      ;;\r\n    r)\r\n      gamma=$OPTARG\r\n      ;;\r\n    \\?)\r\n      echo \"Invalid option: -$OPTARG\"\r\n      ;;\r\n    :)\r\n      echo \"Option -$OPTARG requires an argument.\"\r\n      ;;\r\n  esac\r\ndone\r\n<\/pre>\n<span id=\"grep_Examples\"><h2>grep Examples<\/h2><\/span>\n<pre lang=\"bash\">\r\n# grep one of multiple strings\r\ngrep \"string1\\|string2\\|string3\"\r\n\r\n# Inverse grep\r\ngrep -v \"notThisString\"\r\n<\/pre>\n<span id=\"Remove_Old_Log_Files\"><h2>Remove Old Log Files<\/h2><\/span>\n<pre lang=\"bash\">\r\n# List log files older than 30 days\r\nfind . -type f -mtime +30 -name \"*.log\" -exec ls -altr {} \\;\r\n\r\n# Remove log files older than 30 days\r\nfind . -type f -mtime +30 -name \"*.log\" -exec rm {} \\;\r\n\r\n# Remove log files older than 3 days\r\nfind . -type f -mtime +3 -name \"*.log\" -exec rm {} \\;\r\n\r\n# Find log files less than one day old\r\nfind . -type f -mtime -1 -name \"*.log\" -exec ls -l {} \\;\r\n<\/pre>\n<span id=\"Some_Debug_Methods\"><h2>Some Debug Methods<\/h2><\/span>\n<pre lang=\"bash\">\r\n# --------------------------------------------------------- Constants\r\n# Turn debug on\/off\r\nDEBUG=1\r\n\r\n# Turn dryrun on\/off\r\nDRYRUN=1\r\n\r\n# Turn logfile on\/off\r\nUSELOGFILE=0\r\n\r\npath=\/home\/me\r\n\r\n# --------------------------------------------------------- Debug methods\r\necho_debug() {\r\n\tif [ $DEBUG -eq 1 ]\r\n\tthen\r\n\t\tif [ ! $2 ]\r\n\t\tthen\r\n\t\t\techo \"\" # Add a new line\r\n\t\t\techo \"---> $1\"\r\n\t\telse\r\n\t\t\techo \"         $1\"\r\n\t\tfi\r\n\t\t\r\n\tfi\r\n}\r\n\r\ndry_run() {\r\n\tif [ $DRYRUN -eq 1 ]\r\n\tthen\r\n\t\techo \"Dry run: ${@}\"\r\n\t\treturn 0;\r\n\telse\r\n\t\teval \"${@}\"\r\n\tfi\r\n}\r\n\r\nuse_logfile() {\r\n\tif [ $USELOGFILE -eq 1 ]\r\n\tthen\r\n\t\t# Redirect all outputs to a log file\r\n\t\texec >> $SCRIPT_DIR\/`basename $0`.log 2>&1\r\n\tfi\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>* See this post for more examples. Timestamp ts=`date +%m%d%Y` ts2=`date +%H%M%S_%m%d%Y` Find with Permission Denied Filtered Out find . -name &#8220;something&#8221; -print 2>\/dev\/null > find.something.file Find String in Type of Files #!\/bin\/sh if [ $# -lt 2 ]; then &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1423\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[65],"tags":[],"class_list":["post-1423","post","type-post","status-publish","format-standard","hentry","category-shell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-mX","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1423"}],"version-history":[{"count":21,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1423\/revisions"}],"predecessor-version":[{"id":11350,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1423\/revisions\/11350"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}