{"id":790,"date":"2020-08-23T21:06:28","date_gmt":"2020-08-23T15:36:28","guid":{"rendered":"https:\/\/techieshouts.com\/?p=790"},"modified":"2022-08-09T19:05:08","modified_gmt":"2022-08-09T13:35:08","slug":"passing-arguments-in-shell-script","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/passing-arguments-in-shell-script\/","title":{"rendered":"Passing arguments in shell script"},"content":{"rendered":"\n<p>Command line arguments are essential for any programming or scripting language to have a control of the the input parameters. Passing arguments to the shell script can be done in multiple ways. In this blog, we will see the two ways of passing arguments to a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Shell_script\" target=\"_blank\" rel=\"noopener\">shell script<\/a><\/p>\n\n\n\n<h2>Option 1 &#8211; Using getops<\/h2>\n\n\n\n<p>This method is the most flexible and standard way of passing the arguments. In this method, you can pass the arguments in any order provided you use the corresponding character. Also you can change the number of arguments dynamically.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">usage=\"$(basename $0) -d &lt;Database> -t &lt;TableName> -i &lt;Id> -u &lt;User> -r &lt;RunDate>\"\nwhile getopts \"d:t:r:i:u:s:\" arg; do\n  case ${arg} in\n  d)\n    database=\"${OPTARG}\"\n    ;;\n  t)\n    tablename=\"${OPTARG}\"\n    ;;\n  i)\n    id=\"${OPTARG}\"\n    ;;\n  u)\n    dbuser=\"${OPTARG}\"\n    ;;\n  r)\n    rundate=\"${OPTARG}\"\n    ;;\n  *)\n    echo \"ERROR: Invalid input ${arg}\"\n    echo \"${usage}\"\n    exit 1\n    ;;\n  esac\ndone\ndatabaseuser=${dbuser:-defaultuser}\n\necho \"\"\necho \"Arguments List\"\necho \"--------------\"\necho \"Database\t: $database\"\necho \"Tablename\t: $tablename\"\necho \"Id\t: $id\"\necho \"Dbuser\t: $databaseuser\"\necho \"Rundate\t: $rundate\"\necho \"\"\n\nexit 0<\/pre>\n\n\n\n<p>The echo statements are self-explanatory. Let us see the purpose of other commands in the script.<\/p>\n\n\n\n<p><strong>usage=&#8221;$(basename $0) -d  -t  -i  -u  -r &#8220;<\/strong>  => This variable will hold the warning message that needs to be displayed if a wrong argument is passed.<\/p>\n\n\n\n<p><strong>while getopts &#8220;d:t:r:i:u:s:&#8221; arg;<\/strong> => This will show the list of arguments that can be passed to the program. <\/p>\n\n\n\n<p><strong>case ${arg} in<\/strong> &#8211; This is like the regular switch case statement. Whichever character matches, it will take control to the corresponding case.<\/p>\n\n\n\n<p><strong>databaseuser=${dbuser:-defaultuser}<\/strong> => If the dbuser argument is not passed, this statement will assign the default user. This is a kind of error handling technique too.<\/p>\n\n\n\n<h3>Example 1 &#8211; Passing all arguments<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sh args.sh -d testdb -t testtable -i 10 -r 20200810 -u readuser<\/pre>\n\n\n\n<p>The result of the above script execution is shown below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"godzilla\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Arguments List\n--------------\nDatabase\t: testdb\nTablename\t: testtable\nId\t\t: 10\nDbuser\t\t: readuser\nRundate\t\t: 20200810<\/pre>\n\n\n\n<h3>Example 2 &#8211; Without one argument <\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sh args.sh -d testdb -t testtable -i 10 -r 20200810<\/pre>\n\n\n\n<p>As you can see, we purposely missed the <strong>&#8220;-u readuser&#8221;<\/strong> argument in this case. But,  you can also see that the program smartly setting the default value <strong>&#8220;defaultuser&#8221;<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"godzilla\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Arguments List\n--------------\nDatabase\t: testdb\nTablename\t: testtable\nId\t\t: 10\nDbuser\t\t: defaultuser\nRundate\t\t: 20200810<\/pre>\n\n\n\n<h2>Option 2 &#8211; Directly passing args with space<\/h2>\n\n\n\n<p>In this option, you simply pass all the arguments with space in between them. Inside the program, you can assign the values to the local variables. This will not have the flexibility of sending arguments in a different order and you have to pass all the arguments as well.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">main() {\nusage=\"$(basename $0) database tablename username id rundate\"\necho \"Number of args: $#\"\nif [[ $# -lt 5 ]];then\n   echo \"ERROR: Pass all the args properly.\"\n   echo \"Example - ${usage}\"\n   exit 1\nfi\necho \"Received all the arguments\"\n\necho \"Arguements list\"\necho \"---------------\"\necho \"Database\t: ${1}\"\necho \"Tablename\t: ${2}\"\necho \"Username\t: ${3}\"\necho \"Id\t: ${4}\"\necho \"Rundate\t: ${5}\"\necho \"\"\nexit 0\n}\n\nmain \"$@\"<\/pre>\n\n\n\n<p><strong>if [[ $# -lt 5 ]];then<\/strong> &#8211; This will check if the number of arguments is less than the expectation. This is a predefined number so do not have the flexibility to change it during the run time.<\/p>\n\n\n\n<p><strong>main &#8220;$@&#8221;<\/strong> &#8211; This is the entry of the execution. This will receive all the arguments passed and send it to the main function.<\/p>\n\n\n\n<h3>Example 1 &#8211; Passing all arguments<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sh argsbasic.sh testdb testtable adminuser 50 20200806<\/pre>\n\n\n\n<p>Here we have passed all the arguments in the expected order. Remember if we change the order of arguments, the program will never know. It can only check for a number of arguments. The result is shown below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Number of args: 5\nReceived all the arguments\nArguements list\n---------------\nDatabase\t: testdb\nTablename\t: testtable\nUsername\t: adminuser\nId\t        : 50\nRundate\t        : 20200806<\/pre>\n\n\n\n<h3>Example 2 &#8211; Without one argument<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sh argsbasic.sh testdb testtable adminuser 50<\/pre>\n\n\n\n<p>In this case, we purposely left the last argument to see the behavior of the program. Since the program is expecting 5 arguments, it will throw an error in this case.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Number of args: 4\nERROR: Pass all the args properly.\nExample - argsbasic.sh database tablename username id rundate<\/pre>\n\n\n\n<p>Also, check &#8220;<a href=\"https:\/\/techieshouts.com\/for-loop-in-shell-script-over-string-list\/\">How to iterate through the list using for loop&#8221;<\/a>, &#8220;<a href=\"https:\/\/techieshouts.com\/reading-a-delimited-file-in-shell-script\/\">How to read delimited file in shell script<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Command line arguments are essential for any programming or scripting language to have a control of the the input parameters. Passing arguments to the shell script can be done in multiple ways. In this blog, we will see the two ways of passing arguments to a shell script Option 1 &#8211; Using getops This method\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/passing-arguments-in-shell-script\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,8],"tags":[94,96,95],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/790"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=790"}],"version-history":[{"count":8,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/790\/revisions"}],"predecessor-version":[{"id":833,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/790\/revisions\/833"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}