Posts

Showing posts with the label parameters

Featured Post

Taking inputs from user in a script

Here we will see how to take input from user in a script with the help of read command. #!/bin/bash Lets try multiplication of two numbers which the user enters. What read does is ? It takes values by user on stdin. It prompt the user to input any value on stdin. -p is used to display a prompt. read -p "First number:" value1 This will give a prompt as " First number " and store the value in variable " value1 " echo Will display a blank line. read -p "Second number:" value2 This will give a prompt as " Second number " and store the value in variable " value2 " echo Will display a blank line. read -sn1 -p "Press any key to see the answer..." Just to make it bit more interactive lets add one more thing. -s means silent mode that is if any value is pressed it will not be displayed on the screen like as in the password. -nX means how many characters. X here is one that is any ...

Taking inputs from user in a script

Here we will see how to take input from user in a script with the help of read command. #!/bin/bash Lets try multiplication of two numbers which the user enters. What read does is ? It takes values by user on stdin. It prompt the user to input any value on stdin. -p is used to display a prompt. read -p "First number:" value1 This will give a prompt as " First number " and store the value in variable " value1 " echo Will display a blank line. read -p "Second number:" value2 This will give a prompt as " Second number " and store the value in variable " value2 " echo Will display a blank line. read -sn1 -p "Press any key to see the answer..." Just to make it bit more interactive lets add one more thing. -s means silent mode that is if any value is pressed it will not be displayed on the screen like as in the password. -nX means how many characters. X here is one that is any ...

Positional parameters in Bash scripting

Here we will see the naming convention of parameters which are passed to a script and how we can use them? how shifting works? #!/bin/bash # Positional parameters #       $0          $1     $2    $3   .......   $n # Command  Arg1 Arg2 Arg3 ....... Argn echo -e "First argument $1, Second argument $2, Third argument $3, Fourth argument $4\n" This will print value of all four arguments which you passed, just for your reference. shift The positional parameters from n+1 ... are renamed  to  $1, so that every time after shift is performed there is a new argument to be processed. This will shift to the left by one value. echo "Shifting...." echo -e "First argument $1, second argument $2, third argument $3, fourth argument $4 \n" This will print new values after shifting the arguments by one value. shift 2 This will shift to the left by two values. echo "Shifting.... by 2 vlau...