{"id":336,"date":"2018-02-12T06:37:29","date_gmt":"2018-02-12T06:37:29","guid":{"rendered":"http:\/\/info.juliusgoh.life\/?p=336"},"modified":"2018-04-24T02:50:10","modified_gmt":"2018-04-24T02:50:10","slug":"shell-scripting-accepting-argv-getops-checking-echoing","status":"publish","type":"post","link":"https:\/\/info.juliusgoh.life\/?p=336","title":{"rendered":"Shell Scripting Accepting Argv , getops , checking , echoing"},"content":{"rendered":"<pre>#!\/bin\/bash\r\n\r\n#getopts checking for argv and define it into variables\r\n#options that needs to receive parameter have to enter : after the option EG: f:u:d:\r\n#In This Example h does not accept parameter. it execute the case only\r\n\r\nwhile getopts u:d:p:f:h option\r\n\r\ndo\r\n case \"${option}\"\r\n in\r\n u) USER=${OPTARG};;\r\n d) DATE=${OPTARG};;\r\n p) PRODUCT=${OPTARG};;\r\n f) FORMAT=$OPTARG;;\r\n h) echo \"FORMAT : .\/bash_accept_argv.sh -u {username} -d {date} -p {product} -f {file_type} EG: .\/bash_accept_argv.sh -u 'bot1 bot2' -d '2018-01-05' -p MOMOKO -f pdf \"\r\n    exit 1;; \r\n ?) echo \"FORMAT : .\/bash_accept_argv.sh -u {username} -d {date} -p {product} -f {file_type} EG: .\/bash_accept_argv.sh -u 'bot1 bot2' -d '2018-01-05' -p MOMOKO -f pdf \"\r\n    exit 1;; \r\n esac\r\ndone\r\n\r\n#Prevent Empty option passed into the script\r\n#[[ -z ${USER} ]] - Check length (More than one word)\r\n#[ -z ${FORMAT}]  - Check Length (one word)\r\nif [[ -z ${USER} ]] || [[ -z ${DATE} ]] || [ -z ${PRODUCT} ] || [ -z ${FORMAT} ]\r\nthen\r\n    echo \"FORMAT : .\/bash_accept_argv.sh -u {username} -d {date} -p {product} -f {file_type} EG: .\/bash_accept_argv.sh -u 'bot1 bot2' -d '2018-01-05' -p MOMOKO -f pdf \"\r\n    exit 1\r\nfi\r\n\r\n    \r\n\r\n#How to extract more than one data pass into the option\r\nfor subuser in $USER;do\r\n    echo $subuser\r\ndone;\r\necho ${DATE}\r\necho ${PRODUCT}\r\necho ${FORMAT}\r\n<\/pre>\n<p>ref link : <a href=\"http:\/\/wiki.bash-hackers.org\/howto\/getopts_tutorial\">Click Here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash #getopts checking for argv and define it into variables #options that needs to receive parameter have to enter : after the option EG: f:u:d: #In This Example h does not accept parameter. it execute the case only while getopts u:d:p:f:h option do case &#8220;${option}&#8221; in u) USER=${OPTARG};; d) DATE=${OPTARG};; p) PRODUCT=${OPTARG};; f) FORMAT=$OPTARG;; h) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":214,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[22,2],"tags":[],"_links":{"self":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336"}],"version-history":[{"count":2,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions\/400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/media\/214"}],"wp:attachment":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}