사용자가 디렉터리, 연락처 이름 및 연락처 번호를 추가하도록 요청하는 프로그램을 만들려고합니다.이 스크립트에서 무한 루프가 발생하는 이유는 무엇입니까?
연락처 이름이 이미 사용자가 지정한 디렉토리에있는 경우 파일 이름은 contact (1) .txt이됩니다. 그 파일도 존재하면, 새로운 파일은 이됩니다. 연락처 (2) .txt 등
다음 코드를 실행하려고하면 무한 루프가 발생하지만 버그를 찾으려고 시도했지만 couldn 어떤 것도 찾지 못한다.
if [ "$exists" = 1 ]; then
또한주의 따옴표를 사용하고, 하나의 등호 : 당신은 여기에 공간이 필요
#!/bin/bash
directory=
if(($# == 0))
then
echo -n "Please enter directory path:"
read directory
cd $directory
echo -n "Please enter contact name:"
read name
echo -n "Please enter contact number:"
read number
else
directory=$1
cd ${directory}
name=$2
number=$3
fi
if [ -e $name.contact ];
then
exists=1
count=1
while [ $exists==1 ];
do
name=$name\($count\)
count=$(($count+1))
if [ ! -f $name.contact ];
then
exists=0
echo $exists
fi
done
echo $number > $name.contact
else
echo $number > $name.contact
fi
도움이 되셨을 것입니다. –
while [$ exists == 1];은 [$ exists -eq 1];'이어야합니다. – fedorqui
또한 Bash라면 대괄호 ('[['and']]')를 사용하고 싶을 것입니다. 설명을 보려면 http://mywiki.wooledge.org/BashFAQ/031을 확인하십시오. –