我需要将这个批处理脚本知道为Bash:
@echo off
set /p name= Name?
findstr /m "%name%" ndatabase.txt
if %errorlevel%==0 (
cls
echo The name is found in the database!
pause >nul
exit
)
cls
echo.
echo Name not found in database.
pause >nul
exit
我是Linux Kernel的新手,所以从一个简单的发行版开始 - Ubuntu 12.10。我的问题是我对Bash Script并不是很了解,因为我非常习惯批处理脚本格式;这显然是我的C ++的坏习惯。
答案 0 :(得分:2)
我认为是这样的:
#!/bin/bash
read -p "Name? " name
clear
if [ $(grep -qF "$name" ndatabase.txt) ]
then
read -p "The name is found in the database!" PAUSE
else
read -p "Name not found in database." PAUSE
fi
和更短的版本:
#!/bin/bash
read -p "Name? " name
[ $(grep -qF "$name" ndatabase.txt) ] && echo "The name is found in the database!" || echo "Name not found in database."
答案 1 :(得分:0)
Umm“Korn shell”与Bash密切相关(参见Rosenblatt撰写的'学习Korn Shell')
@echo off ::: has no linux/bash meaning
set /p name= Name? ::: read input from prompt 'name?' --- _'echo -n "name?"; read TERM;'_
findstr /m "%name%" ndatabase.txt ::: _grep $TERM ndatabase.txt_
if %errorlevel%==0 ::: _if (($! == 0 )); then_ enclosed with _fi_; the (( means numeric
cls ::: no real linux/bash meaning (maybe multiple _echo_ statements
pause ::: can be faked with _read_
_exit_ translates rather well.
_$$_=process id (aka pid) _$@_ commandline arguments, there are others : _$!_ is last exit code
var=_$(command)_ var (with **NO** dollarsign assumes the contents of _command_'s output
答案 2 :(得分:0)
我想这是尽可能直接的:
#!/bin/bash
read -p "Name? " name
fgrep -le "$name" ndatabase.txt
if [ $? = 0 ]; then
clear
echo "The name is found in the database!"
read -n1 >/dev/null
exit
fi
clear
echo
echo "Name not found in database."
read -n1 >/dev/null
exit
正如其他人所说,虽然有更短,更优雅的解决方案。