1

我需要知道这个批处理脚本到 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 内核的新手,所以从一个简单的发行版开始 - Ubuntu 12.10。我的问题是我不太了解 Bash Script,因为我非常习惯 Batch Script 格式;这对我的 C++ 来说显然是个坏习惯。

4

3 回答 3

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."
于 2012-12-16T04:45:59.213 回答
0

嗯,“Korn shell”与 Bash 密切相关(参见 Rosenblatt 的 oreilly 'Learning the 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
于 2012-12-16T04:40:05.060 回答
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

正如其他人所提到的,尽管有更短、更优雅的解决方案。

于 2012-12-16T05:36:09.600 回答