Shell Script Examples: case, count, for, if, while and User input

来源:互联网 发布:excel多个表格数据求和 编辑:程序博客网 时间:2024/04/29 01:11

原文地址: http://rhau.se/2009/04/17/shell-script-examples-case-count-for-if-while-and-user-input/


If you, like so many other people these days, switch to Linux you will come in contact with something called unux scripts, shell scripts or bash scripts. These scripts are text-files that execute commands just like bat-files in Windows. I prefer shell-scripts because i think they are more powerful then bat-files but that is just my opinion.

Some things you should know:
#!/bin/bash usually is the first line in a bash script. It is called shebang (also called a hashbang, hashpling, or pound bang) and specifies in which shell to execute the code that follows below.

Everything after a # is a comment and does not get executed.

Below you will find examples of a few things that you can do with shell scripts on Linux and other Unixes. If you have questions: have a look at the man page of the command or post a comment and I will try to explain.

case

123456789101112131415161718192021222324252627282930313233343536
#!/bin/bash MENU="1   Date and Time2   Calendar for current month3   quit" while true; do  clear  echo "$MENU"  echo -n "Please make your choice: "  read INPUT # Read user input and assign it to variable INPUT   case $INPUT in    1)    date    echo press ENTER to continue    read    ;;    2)    cal    echo press ENTER to continue    read    ;;    3|q|Q) # If user presses 3, q or Q we terminate    exit 0    ;;    *) # All other user input results in an usage message    clear    echo Please choose alternatves 1, 2 or 3    sleep 2    ;;  esac done

count

12345678910
#!/bin/bashBEGIN=1 # Start counting hereEND=10 # Stop counting here END=`expr $END + 1` while [ $END -ne $BEGIN ]; do # While END is not equal to BEGIN do ...  echo This is iteration $BEGIN  BEGIN=`expr $BEGIN + 1` # Increasing the value of BEGIN by onedone

for

12345678910111213141516
#!/bin/bash # Defining VariableSTAFF="Anders Bertil Cesar Denise Emilia Filippa" echo First LOOPfor PERSON in $STAFF; do  echo $PERSON is a member of the staff.done echo Second LOOPecho -n "The staff members are: "for PERSON in $STAFF; do  echo -n "$PERSON " # -n instructs echo not to print the trailing newline chardoneecho

if

12345678910111213141516
#!/bin/bashVALIDUSER="Thorsten" if [ $VALIDUSER = anders ]; then  echo $VALIDUSER is a valid user  exit 0elif [ $VALIDUSER = bertil ]; then  echo $VALIDUSER is a valid user  exit 0elif [ $VALIDUSER = Thorsten ]; then  echo $VALIDUSER is a valid user  exit 0else  echo no valid user found  exit 1fi

while

12345678910
#!/bin/bash START=1 # The value to begin withEND=10  # When this value is reached we are doneSPEED=1 # Number of seconds between each iterationwhile [ $START -le $END ]; do  echo this is iteration $START of $END  START=`expr $START + 1`  sleep $SPEEDdone

User Input

12345678910111213141516171819
#!/bin/bash # Check if there is input to this scriptif [ $# -lt 1 ]; then # $# contains the total number of arguments to the script  echo "Usage: $0 [argument1 ...]" # $0 is the executed command  exit 1fi NUMPARAMETERS=`echo $#` # The number of parameters provded to this script echo "This script has $NUMPARAMETERS parameters" COUNT=1 while [ $COUNT -le $NUMPARAMETERS ]; do  echo Parameter $COUNT contains $1 # $1 is the first argument  COUNT=`expr $COUNT + 1` # Increasing COUNT by one  shift # Shifts input parameters. $2 becomes $1 and the old $1 is discardeddone

原创粉丝点击