스크립팅 질문

스크립팅 질문

사용자에게 파일 이름을 요청한 다음 해당 파일을 생성하는 스크립트를 어떻게 생성합니까?

나는 이미 이것을 시도했습니다:

#!/bin/bash
#Get Script name of this file and create it
#Get Author's name of this file
#Add the date this file is ran
#Add a "Hello"

echo -n "Please type Script destination file name: "
read 0

echo -n "Please type your name: "
read name

if [ -z "$0" ]
then
    echo "No Script name given."
else
    echo "#Script: $0" > $0
fi

if [ -z "$name" ]
then
    echo "No name given."
else
    echo "#Author: $name" >> $0
    echo "#Date: `date`"  >> $0
fi

답변1

문제는 변수 이름으로 $0을 사용하는 데 있습니다. 이는 호출되는 스크립트의 이름을 위해 예약되어 있습니다. . echo $0;​또한 이 이상의 각 정수 변수는 스크립트에 전달된 인수용으로 예약되어 있습니다.

다음은 예상대로 작동하는 스크립트의 업데이트된 버전입니다. $0을 $filename으로, $name을 $username으로 변경했습니다.

#!/bin/bash
#Get Script name of this file and create it
#Get Author's name of this file
#Add the date this file is ran
#Add a "Hello"

echo -n "Please type Script destination file name: "
read filename

echo -n "Please type your name: "
read username

if [ -z "$filename" ]
then
    echo "No Script name given."
else
    echo "#Script: $filename" > $filename
fi

if [ -z "$username" ]
then
    echo "No name given."
else
    echo "#Author: $username" >> $filename
    echo "#Date: `date`"  >> $filename
fi

관련 정보