{"id":533,"date":"2016-11-29T22:57:03","date_gmt":"2016-11-29T17:27:03","guid":{"rendered":"http:\/\/www.cyberaka.com\/?p=533"},"modified":"2016-11-29T22:57:03","modified_gmt":"2016-11-29T17:27:03","slug":"user-input-in-shell-script","status":"publish","type":"post","link":"https:\/\/www.cyberaka.com\/?p=533","title":{"rendered":"User input in shell script"},"content":{"rendered":"<p>I needed to write a small shell script which would allow me to make some decision based on user choices. I needed the user to specify Yes, No or Cancel for an operation. The following piece of hack does the job well.<\/p>\n<p><code>#! \/bin\/bash<\/p>\n<p># define constants<br \/>\ndeclare -r TRUE=0<br \/>\ndeclare -r FALSE=1<\/p>\n<p>user_choice() {<br \/>\n  local str=\"$@\"<br \/>\n  while true<br \/>\n  do<br \/>\n    # Prompt user, and read command line argument<br \/>\n    read -p \"$str \" answer<\/p>\n<p>    # Handle the input we were given<br \/>\n    case $answer in<br \/>\n     [yY]* ) return $TRUE;;<\/p>\n<p>     [nN]* ) return $FALSE;;<\/p>\n<p>     [cC]* ) exit;;<\/p>\n<p>     * )     echo \"Y - Yes, N - No, C - Cancel. Please choose valid option.\";;<br \/>\n    esac<br \/>\n  done<br \/>\n}<\/p>\n<p>if user_choice \"Execute Job 1? \"; then<br \/>\n  echo \"Executing Job 1..\"<br \/>\nfi<br \/>\nif user_choice \"Execute Job 2? \"; then<br \/>\n  echo \"Executing Job 2..\"<br \/>\nfi<br \/>\n<\/code><\/p>\n<p>The following links were referred to achieve this little script:<br \/>\nhttp:\/\/linuxcommand.org\/wss0090.php<br \/>\nhttps:\/\/bash.cyberciti.biz\/guide\/Returning_from_a_function<br \/>\nhttp:\/\/alvinalexander.com\/linux-unix\/shell-script-how-prompt-read-user-input-bash<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed to write a small shell script which would allow me to make some decision based on user choices. I needed the user to specify Yes, No or Cancel for an operation. The following piece of hack does the job well. #! \/bin\/bash # define constants declare -r TRUE=0 declare -r FALSE=1 user_choice() { [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,35],"tags":[],"class_list":["post-533","post","type-post","status-publish","format-standard","hentry","category-linux-os","category-mac-os"],"_links":{"self":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":1,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":534,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/534"}],"wp:attachment":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}