写一个脚本实现如下功能: 输入一个数字,然后运行对应的一个命令。显示命令如下:*cmd meau** 1---date 2--ls 3--who 4--pwd
当输入1时,会运行date, 输入2时运行ls, 依此类推。
参考脚本:
{{{密码回复可见}}}
当输入1时,会运行date, 输入2时运行ls, 依此类推。
参考脚本:
{{{密码回复可见}}}
0
#! bin/bash
read -p "Input a number:" a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo:"*cmd meau**"
;;
esac
read -p "Input a number:" a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo:"*cmd meau**"
;;
esac
0
本帖最后由 SHMILY 于 2014-1-4 23:31 编辑
#这是个shell脚本练习题
#! /bin/bash
read -p "请输入数字1-4:" a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
#这是个shell脚本练习题
#! /bin/bash
read -p "请输入数字1-4:" a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
0
本帖最后由 chekir 于 2014-1-6 14:05 编辑
- #! /bin/bash
- echo "*Cmd meau*"
- echo "1--date;2--pwd;3--who;4--pwd"
- while :; do
- read -p "Please input a Num:" i
- if [ $i -eq 1 ]; then
- echo `date`
- elif [ $i -eq 2 ]; then
- echo `ls`
- elif [ $i -eq 3 ]; then
- echo `who`
- elif [ $i -eq 4 ]; then
- echo `pwd`
- else
- echo 'Please input 1-4,Thanks!'
- fi
- done
0
#!/bin/bash
read -p "Please input a number between 1 and 4 :" i
case $i in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "The number you input is invalid! Please input again."
;;
esac
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "Please input a number between 1 and 4 :" i
case $i in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "The number you input is invalid! Please input again."
;;
esac
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
0
方法一:if
#!/bin/bash
#this is command list
read -p 'please input a numbaer 1-4:' i
if [ $i -eq 1 ]; then
date +%c
elif [ $i -eq 2 ]; then
ls
elif [ $i -eq 3 ]; then
who
elif [ $i -eq 4 ]; then
pwd
else
echo 'please input a number 1-4:'
fi
----------------------------------------------------------------------------------------------------------------------------
方法二: case
#!/bin/bash
read -p "please input a number 1-4:" n
case $n in
1)
date +%c
;;
2)
ls
;;
3)
w
;;
4)
pwd
;;
*)
echo 'please input a number 1-4:'
esac
#!/bin/bash
#this is command list
read -p 'please input a numbaer 1-4:' i
if [ $i -eq 1 ]; then
date +%c
elif [ $i -eq 2 ]; then
ls
elif [ $i -eq 3 ]; then
who
elif [ $i -eq 4 ]; then
pwd
else
echo 'please input a number 1-4:'
fi
----------------------------------------------------------------------------------------------------------------------------
方法二: case
#!/bin/bash
read -p "please input a number 1-4:" n
case $n in
1)
date +%c
;;
2)
ls
;;
3)
w
;;
4)
pwd
;;
*)
echo 'please input a number 1-4:'
esac
0
#! /bin/bash/
#输入一个数字,然后运行对应的一个命令。显示命令如下:*cmd meau** 1---date 2--ls 3--who 4--pwd
read -p "输入一个数字" n
case $n
1) date
;;
2) ls
;;
3) who
;;
4) pwd
;;
*) echo "输入有误";
;;
esac
#输入一个数字,然后运行对应的一个命令。显示命令如下:*cmd meau** 1---date 2--ls 3--who 4--pwd
read -p "输入一个数字" n
case $n
1) date
;;
2) ls
;;
3) who
;;
4) pwd
;;
*) echo "输入有误";
;;
esac
0
[root@OceanV sh]# cat case.sh
#!/bin/bash
#输入一个数字,然后运行对应的一个命令。
#显示命令如下:*cmd meau** 1---date 2--ls 3--who 4--pwd
#当输入1时,会运行date, 输入2时运行ls, 依此类推。
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "please input a number(1-4):" i
case $i in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "illegal input,please check!"
;;
esac
#!/bin/bash
#输入一个数字,然后运行对应的一个命令。
#显示命令如下:*cmd meau** 1---date 2--ls 3--who 4--pwd
#当输入1时,会运行date, 输入2时运行ls, 依此类推。
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "please input a number(1-4):" i
case $i in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "illegal input,please check!"
;;
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
read -p "Please input a num,range 1-4 :" n
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
case `echo $n` in
1)
echo $(date)
;;
2)
echo $(ls)
;;
3)
echo $(who)
;;
4)
echo $(pwd)
;;
*)
echo "error,please input a num again,range 1-4"
esac
0
#!/bin/bash
echo "please input your cmd num:"
echo "cmd 1--date 2--ls 3--who 4--pwd"
read num;
case $num in
1)
date ;;
2)
ls ;;
3)
who ;;
4)
pwd ;;
*)
echo "it is not the right cmd numuber"
esac
echo "please input your cmd num:"
echo "cmd 1--date 2--ls 3--who 4--pwd"
read num;
case $num in
1)
date ;;
2)
ls ;;
3)
who ;;
4)
pwd ;;
*)
echo "it is not the right cmd numuber"
esac
0
#!/bin/bash
## This script is for run command through a number in list.
## Writed by Louis on 2014/08/31 10:25
if_num(){
m=`echo $1|sed 's/[0-9]//g'`
if [ -z $m ]; then
return 0
else
return 2
fi
}
echo "Menu"
echo "***********"
echo "1:date"
echo "2:ls"
echo "3:who"
echo "4:pwd"
echo "***********"
while :; do
read -p "Please input a number(1-4): " n
if if_num $n; then
if [ $n -ge 1 ] && [ $n -le 4 ]; then
case $n in
1)
date
exit
;;
2)
ls
exit
;;
3)
who
exit
;;
4)
pwd
exit
;;
esac
else
echo "The number you input is out of range!"
continue
fi
else
echo "You should input a number!"
continue
fi
done
#!/bin/bash
## This script is for run command through a number in list.
## Writed by Louis on 2014/08/31 10:25
if_num(){
m=`echo $1|sed 's/[0-9]//g'`
if [ -z $m ]; then
return 0
else
return 2
fi
}
echo "Menu"
echo "***********"
echo "1:date"
echo "2:ls"
echo "3:who"
echo "4:pwd"
echo "***********"
while :; do
read -p "Please input a number(1-4): " n
if if_num $n; then
if [ $n -ge 1 ] && [ $n -le 4 ]; then
case $n in
1)
date
exit
;;
2)
ls
exit
;;
3)
who
exit
;;
4)
pwd
exit
;;
esac
else
echo "The number you input is out of range!"
continue
fi
else
echo "You should input a number!"
continue
fi
done
0
#!/bin/bash
echo "*cmd meau** 1--date
2--ls
3--who
4--pwd"
read -p "plz input a number(1-4) :" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "Input a number!eg.1-4"
;;
esac
echo "*cmd meau** 1--date
2--ls
3--who
4--pwd"
read -p "plz input a number(1-4) :" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "Input a number!eg.1-4"
;;
esac
0
- #!/bin/bash
- # Test script.
- # Wirten by Wangxiaoqiang 2014-12-01.
- read -p "Please input number: " num
- case $num in
- 1)
- date
- ;;
- 2)
- ls
- ;;
- 3)
- who
- ;;
- 4)
- pwd
- ;;
- *)
- echo "Hello shell"
- ;;
- esac
0
#!/bin/bash
echo "************"
echo "* cmd meau *"
echo "* 1--date *"
echo "* 2--ls *"
echo "* 3--who *"
echo "* 4--pwd *"
echo "************"
read -p "please input your choice: " num
case $num in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
echo "************"
echo "* cmd meau *"
echo "* 1--date *"
echo "* 2--ls *"
echo "* 3--who *"
echo "* 4--pwd *"
echo "************"
read -p "please input your choice: " num
case $num in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
0
#!/bin/bash
#
function select_command()
{
case $1 in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "please enter a number less than 5"
;;
esac
}
read -p "*cmd meau** 1---date 2--ls 3--who 4--pwd :" n
select_command $n
#
function select_command()
{
case $1 in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "please enter a number less than 5"
;;
esac
}
read -p "*cmd meau** 1---date 2--ls 3--who 4--pwd :" n
select_command $n
0
#!/bin/bash
echo "*cmd menu* 1-date 2-ls 3-who 4-pwd"
read -p "please input a number between 1 to 4:" i
case $i in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "The number you input is invalid! please input again"
;;
esac
echo "*cmd menu* 1-date 2-ls 3-who 4-pwd"
read -p "please input a number between 1 to 4:" i
case $i in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "The number you input is invalid! please input again"
;;
esac
0
while : ;
do
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "please write 1-4:" n
case $n in
1)
echo "date"
;;
2)
echo "ls"
;;
3)
echo "who"
;;
4)
echo "pwd"
;;
*)
exit
;;
esac
done
do
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "please write 1-4:" n
case $n in
1)
echo "date"
;;
2)
echo "ls"
;;
3)
echo "who"
;;
4)
echo "pwd"
;;
*)
exit
;;
esac
done
0
#!/bin/bash
read -p "please input a number 1-4:" n
if [ $n -eq 1 ]; then
date
elif [ $n -eq 2 ]; then
ls
elif [ $n -eq 3 ]; then
who
elif [ $n -eq 4 ]; then
pwd
else
echo `please input a number 1-4:`
fi
read -p "please input a number 1-4:" n
if [ $n -eq 1 ]; then
date
elif [ $n -eq 2 ]; then
ls
elif [ $n -eq 3 ]; then
who
elif [ $n -eq 4 ]; then
pwd
else
echo `please input a number 1-4:`
fi
0
#!/bin/bash
while:
do
read -p "请输入数字1-4:" n
case $n in
1)
date
exit
;;
2)
ls
exit
;;
3)
who
exit
;;
4)
pwd
exit
;;
*)
echo "输入的数字有误,重新输入:"
;;
esca
done
while:
do
read -p "请输入数字1-4:" n
case $n in
1)
date
exit
;;
2)
ls
exit
;;
3)
who
exit
;;
4)
pwd
exit
;;
*)
echo "输入的数字有误,重新输入:"
;;
esca
done
0
不知道写的怎么样,现丑了
#!/bin/bash
echo "*cmd meau**
1---date
2--ls
3--who
4--pwd"
read -p "please input a number:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
#!/bin/bash
echo "*cmd meau**
1---date
2--ls
3--who
4--pwd"
read -p "please input a number:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
0
read -t 10 -p "Please input a number from '1-4':" n
case $n in
1)
date
exit
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "*cmd meau**"
;;
esac
read -t 10 -p "Please input a number from '1-4':" n
case $n in
1)
date
exit
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "*cmd meau**"
;;
esac
0
- #!/usr/bin/env python
- # coding=utf-8
- import os
- import sys
- print '-----*cmd menu*-----'
- print '1---date'
- print '2---ls'
- print '3---who'
- print '4---pwd'
- print '--------------------'
- while True:
- try:
- input = int(raw_input('璇疯緭鍏ヤ竴涓?暟瀛?'))
- except KeyboardInterrupt:
- sys.exit('\n')
- if input == 1:
- print os.system('date')
- if input == 2:
- print os.system('ls')
- if input == 3:
- print os.system('who')
- if input == 4:
- print os.system('pwd')
0
read -p "input num:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "input error"
;;
esac
read -p "input num:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "input error"
;;
esac
0
#/bin/bash
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "输入1-4:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
echo "*cmd meau** 1---date 2--ls 3--who 4--pwd"
read -p "输入1-4:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
esac
0
#!/bash/bin read -p "Please input number:" a case $a in 1) date ;; 2) ls ;; 3) who ;; 4) pwd ;; esac
0
本帖最后由 hlymlv 于 2016-1-11 17:30 编辑
#!/bin/bash
echo "please select which do you want to running:"
echo "please enter \"Enter\" to show menu:"
select n in date ls who pwd quit
do
case $n in
date)
date
;;
ls)
ls
;;
who)
who
;;
pwd)
pwd
;;
quit)
echo "Bye."
exit
;;
esac
echo "please select which do you want to running:"
echo "please enter \"Enter\" to show menu:"
done额,跑题了
#!/bin/bash
echo "please select which do you want to running:"
echo "please enter \"Enter\" to show menu:"
select n in date ls who pwd quit
do
case $n in
date)
date
;;
ls)
ls
;;
who)
who
;;
pwd)
pwd
;;
quit)
echo "Bye."
exit
;;
esac
echo "please select which do you want to running:"
echo "please enter \"Enter\" to show menu:"
done额,跑题了
0
- #!/bin/bash
- read -p "cmd menu:1-date 2-ls 3-who 4-pwd: " commd
- case $commd in
- 1)
- date +%T
- ;;
- 2)
- ls
- ;;
- 3)
- who
- ;;
- 4)
- pwd
- ;;
- *)
- echo "please input 1-4"
- ;;
- esac
0
#!/bin/bash
echo "1--date 2--ls 3--who 4--pwd"
select ml in 1 2 3 4
do
case $ml in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo " 1 2 3 4 only "
esac
done
echo "1--date 2--ls 3--who 4--pwd"
select ml in 1 2 3 4
do
case $ml in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo " 1 2 3 4 only "
esac
done
0
#! bin/bash
read -p "please input a number *cmd meau** 1---date 2--ls 3--who 4--pwd :" a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo:"*cmd meau**"
;;
esac
#! bin/bash
read -p "please input a number *cmd meau** 1---date 2--ls 3--who 4--pwd :" a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo:"*cmd meau**"
;;
esac
0
read -p "Input number *cmd meau**:" n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "Input number is not meau"
;;
esac
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "Input number is not meau"
;;
esac
0
#!/bin/bash
read -p "input cmd number:" cmd
case "$cmd" in
1)
/bin/date
;;
2)
/bin/ls
;;
3)
/usr/bin/who
;;
4)
/bin/pwd
;;
*)
echo $"Usage: $0 {1|2|3|4}"
esac
exit $?
read -p "input cmd number:" cmd
case "$cmd" in
1)
/bin/date
;;
2)
/bin/ls
;;
3)
/usr/bin/who
;;
4)
/bin/pwd
;;
*)
echo $"Usage: $0 {1|2|3|4}"
esac
exit $?
0
#!/bin/bash
read -p "please input a number: " a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "cmd meau"
;;
Esac
read -p "please input a number: " a
case $a in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "cmd meau"
;;
Esac
0
这段写得不错, 简洁直观, 也考虑了错误处理。
tulip 发表于 2014-1-8 17:12
#!/bin/bash
read -p "Please input a number between 1 and 4 :" i
这段写得不错, 简洁直观, 也考虑了错误处理。
0
#/bin/bash
####ss
##by Lv.
read -p "input your words to select: " n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "only 1|2|3|4 can be input,please retry it"
;;
esac
####ss
##by Lv.
read -p "input your words to select: " n
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
echo "only 1|2|3|4 can be input,please retry it"
;;
esac
0
#!/bin/sh
cat <
*cmd menu*
1.---date
2.---ls
3.---who
4.---pwd
eof
while [ 1 ] ; do
read -p "Please input a number==>" n
case $n in
1) date;;
2) ls;;
3) who;;
4) pwd;;
*) exit 1
esac
done
cat <
1.---date
2.---ls
3.---who
4.---pwd
eof
while [ 1 ] ; do
read -p "Please input a number==>" n
case $n in
1) date;;
2) ls;;
3) who;;
4) pwd;;
*) exit 1
esac
done
0
#/bin/bash #case while : ; do read -p "please in put a number :" a case $a in 1) date ;; 2) ls ;; 3) who ;; 4) pwd ;; *) echo "Please input a number: 1-4" ;; esac done
0
- #!/bin/bash
- ####test 1 to 4 option
- ###by Wang
- while :;
- do
- read -p "input 1 to 4 between:" a
- n=`echo $a |sed 's/[^0-9]//g'`
- case $n in
- 1)
- echo "*cmd meau** : `date`"
- exit
- ;;
- 2)
- echo "*cmd meau**: `ls`"
- exit
- ;;
- 3)
- echo "*cmd meau**: `who`"
- exit
- ;;
- 4)
- echo "*cmd meau**: `pwd`"
- exit
- ;;
- *)
- echo "u input is not for us option"
- if [ -z $n ]
- then
- echo "please input again"
- elif [ $n -gt 5 ]
- then
- exit
- fi
- ;;
- esac
- done
0
- #/bin/bash
- while [[ 1=1 ]]
- do
- read -p "请输入一个数字1---date 2--ls 3--who 4--pwd:" n
- if [[ $n -eq 1 ]]
- then
- date
- break
- elif [[ $n -eq 2 ]]
- then
- ls
- break
- elif [[ $n -eq 3 ]]
- then
- who
- break
- elif [[ $n -eq 4 ]]
- then
- pwd
- break
- else
- echo
- fi
- done
0
#!/bin/bash
#命令选择
read -p "*cmd meau** 1---date 2--ls 3--who 4--pwd: " cmd
if [ $cmd == 0 ]; then
echo "Plesae input number 1-4"
elif [ $cmd == 1 ]; then
/bin/date
elif [ $cmd == 2 ]; then
/bin/ls
elif [ $cmd == 3 ]; then
/usr/bin/who
elif [ $cmd == 4 ]; then
/bin/pwd
else
echo "Plesae input number 1-4"
fi
#命令选择
read -p "*cmd meau** 1---date 2--ls 3--who 4--pwd: " cmd
if [ $cmd == 0 ]; then
echo "Plesae input number 1-4"
elif [ $cmd == 1 ]; then
/bin/date
elif [ $cmd == 2 ]; then
/bin/ls
elif [ $cmd == 3 ]; then
/usr/bin/who
elif [ $cmd == 4 ]; then
/bin/pwd
else
echo "Plesae input number 1-4"
fi
编辑回复