1

I need to know this Batch Script into 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

I am new to the Linux Kernel, so starting off with an easy distro - Ubuntu 12.10. My problem is that I do not really know much of Bash Script, since I am very accustomed to the Batch Script format; which is obviously a bad habit for my 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 シェル」は 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 に答える