简体   繁体   English

UNIX Shell脚本

[英]UNIX shell scripting

currently I'm working on 2 code : 目前我正在研究2代码:

the first code is a code to display a menu to the user (start.sh) : 第一个代码是向用户显示菜单的代码(start.sh):

#!/bin/sh
echo choose a number :
echo "1.)display place information"
echo "2.)look for first character entered"

read number

case $number in
"1") ./script.sh;;
"2") ./script2.sh;;
esac
exit

the code for script.sh is : script.sh的代码是:

#!/bin/sh
grep "$1" Place.txt

basically, when running script.sh , I need to enter 1 argument like ./script.sh home and it match the word 'home' in Place.txt. 基本上,运行script.sh时,我需要输入1个参数,例如./script.sh home ,它与Place.txt中的“ home”一词匹配。

But , when i run the "start.sh" script and choose number 1 which run "script.sh" script I have nowhere to enter the argument (home) and the "script.sh" code won't run. 但是,当我运行“ start.sh”脚本并选择运行“ script.sh”脚本的数字1时,我无处可输入参数(主目录),“ script.sh”代码将无法运行。

any suggestion how to achieve that? 任何建议如何实现?

You can either read the input from start.sh: 您可以从start.sh中读取输入:

start.sh: start.sh:

#!/bin/sh
echo choose a number :
echo "1.)display place information"
echo "2.)look for first character entered"

read number

case $number in
"1") echo "Please input argument"; read p; ./script.sh "$p";;
"2") echo "Please input argument"; read p; ./script2.sh "$p";;
esac
exit

Or do it in script.sh: 或者在script.sh中执行:

script.sh script.sh

#!/bin/sh
echo "Please input argument"
read p
grep "$p" Place.txt

Just dont do both. 只是两者都不做。

why do you want to call another shell script when you can do it in one? 为什么要在一个脚本中同时调用另一个shell脚本?

echo choose a number :
echo "1.)display place information"
echo "2.)look for first character entered"

read number

case $number in
1) read -p "Enter pattern to search: " search
   grep $search file.txt
   ;;
2) ;;
esac
exit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM