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
- Shell Script Examples: case, count, for, if, while and User input
- shell if case while select for
- shell 语句 if for while until case
- Shell Script While Loop Examples
- Shell中关于if,case,for,while等的总结
- shell脚本if case for while语句小例子
- Shell 循环语句:if,case esac,for,while
- shell学习-9-if-case-for-while-until-break
- SHELL脚本语句 if、for、while、case 图示
- Shell中关于if,case,for,while等的总结
- The sample Loadrunner script below shows examples of FOR, DO and DO WHILE LOOPs.
- if for while util case select 参数
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- asp.net页面使用script后会改变样式
- MVC3 get_ViewBag报错解析
- LVS中前置机LB的容错机制
- 客户端和服务端通讯的N种方式(三)
- poi的autoSizeColumn方法对全角或者说中文支持不好的一个解决办法
- Shell Script Examples: case, count, for, if, while and User input
- NoSQL
- EXT4写文件流程
- OS Guide
- SAP发送的mail中加载图片
- android updater-script解析
- kettle 增量更新
- 直方图规范化用于图像增强 matlab程序
- 北京爱立信的面试题