Korn Shell에서 문자열 연결을 인쇄하는 방법

Korn Shell에서 문자열 연결을 인쇄하는 방법

이건 내 스크립트야

[root@localhost scripts]# cat nested.sh
#!/bin/ksh

echo Enter the level of nesting
read lev
echo Enter the directory \( Enter the Absolute Path\)
read path
echo Enter the directory name
read $dirname
cd $path
for((i=1;i<=$lev;i++));
 do
  mkdir '$dirname$i'
  cd '$dirname$i'
 done
echo $dirname$i

$dirname의 최종 값이 "fold"이고 $i의 값이 "5"라고 가정합니다. 마지막 명령문이 Fold5 를 인쇄할 것으로
예상했지만 5만 인쇄합니다.echo $dirname$i

누군가 "fold5"를 인쇄하는 방법을 설명해 줄 수 있나요?
또한 누군가 왜 나에게 5만 인쇄되는지 설명할 수 있습니까?

답변1

스크립트의 8번째 줄에 오타가 있습니다. 다음과 같아야 합니다. read dirname $dirname이 비어 있기 때문에 "5"만 인쇄하는 이유입니다.

이렇게 하면 read $dirname쉘이 '$dirname'을 빈 값으로 확장합니다.

또한 변수를 둘러쌀 때는 항상 큰따옴표를 사용합니다.

수정된 스크립트:

#!/bin/ksh

echo Enter the level of nesting
read lev
echo Enter the directory \( Enter the Absolute Path\)
read path
echo Enter the directory name
read dirname
cd $path
for((i=1;i<=$lev;i++));
 do
  mkdir "$dirname$i"
  cd "$dirname$i"
 done
echo "$dirname$i"

관련 정보