구문 및 기본 이름에 대한 Bash 쉘 스크립트 기본 질문

구문 및 기본 이름에 대한 Bash 쉘 스크립트 기본 질문

다음 스크립트를 고려해보세요.

myname=`basename $0`;
for i in `ls -A`
do
 if [ $i = $myname ]
 then
  echo "Sorry i won't rename myself"
 else
  newname=`echo $i |tr a-z A-Z`
  mv $i $newname
 fi
done

basename $01) 이것이 내 스크립트 이름을 나타낸다는 것을 알고 있습니다 . 하지만 어떻게? 문법을 설명해주세요. 무슨 뜻이에요 $0?

2) 스크립트에서 명령문 끝에 ";"은 언제 사용됩니까? 예를 들어 스크립트의 첫 번째 줄은 ;로 끝납니다. , 8번째 줄은 그렇지 않습니다. 또한 특정 줄(예: 1/6/8 줄) 끝에 세미콜론을 추가/제거하는 것이 실제로 의미가 없다는 것을 알았습니다. 스크립트는 세미콜론이 있든 없든 잘 실행됩니다.

답변1

$0내부 bash 변수입니다. 에서 man bash:

   0      Expands  to  the  name  of  the shell or shell
          script.  This is set at shell  initialization.
          If bash is invoked with a file of commands, $0
          is set to the name of that file.  If  bash  is
          started  with the -c option, then $0 is set to
          the first argument after the string to be exe‐
          cuted,  if  one  is present.  Otherwise, it is
          set to the file name used to invoke  bash,  as
          given by argument zero.

따라서 $0스크립트의 전체 이름은 입니다(예 /home/user/scripts/foobar.sh: . 일반적으로 전체 경로는 필요하지 않고 스크립트 자체의 이름만 필요하므로 basename경로 제거를 사용할 수 있습니다.

#!/usr/bin/env bash

echo "\$0 is $0"
echo "basename is $(basename $0)"

$ /home/terdon/scripts/foobar.sh 
$0 is /home/terdon/scripts/foobar.sh
basename is foobar.sh

;이는 동일한 줄에 여러 명령문을 작성하는 경우 bash에서만 실제로 필요합니다. 귀하의 예에서는 어느 곳에서도 필요하지 않습니다.

#!/usr/bin/env bash

## Multiple statements on the same line, separate with ';'
for i in a b c; do echo $i; done

## The same thing on  many lines => no need for ';'
for i in a b c
do 
  echo $i
done

답변2

무슨 뜻이에요 $0?

man bash, 섹션 PARAMETERS, 하위 섹션 Special Parameters:

   0      Expands to the name of the shell or shell script.  This  is  set
          at shell initialization.  If bash is invoked with a file of com‐
          mands, $0 is set to the name of that file.  If bash  is  started
          with  the  -c option, then $0 is set to the first argument after
          the string to be executed, if one is present.  Otherwise, it  is
          set  to  the file name used to invoke bash, as given by argument
          zero.

거기라고 되어 0있는데, 매개변수가 식별되었기 $0때문이라는 뜻입니다 .$

검색 키를 사용하면 매뉴얼 페이지에서 이 정보를 쉽게 찾을 수 있습니다 /. 입력 /\$0하고 Enter 키를 누르시면 됩니다. 이 경우 $검색 시 특별한 의미가 있으므로 따옴표로 묶어야 하며 , 이 특별한 의미를 "이스케이프"하려면 백슬래시가 필요합니다.\$$

스크립트에서 명령문 끝에 ";"이 사용되는 경우는 언제입니까?

일반적으로 한 줄에 두 개 이상의 명령문을 입력하려는 경우에만 예를 들어 다음을 사용할 수 있습니다 ;.

if [ $i = $myname ]; then

예제 스크립트에는 ;필수 대소문자 가 없으므로 첫 번째 줄에서 제거할 수 있습니다. 세부정보를 다시 확인할 수 있습니다 man bash.

Lists
   A  list  is a sequence of one or more pipelines separated by one of the
   operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
   <newline>.

   [...]

   A sequence of one or more newlines may appear in a list  instead  of  a
   semicolon to delimit commands.

   If  a  command  is terminated by the control operator &, the shell exe‐
   cutes the command in the background in a subshell.  The shell does  not
   wait  for  the command to finish, and the return status is 0.  Commands
   separated by a ; are executed sequentially; the shell  waits  for  each
   command  to terminate in turn.  The return status is the exit status of
   the last command executed.

관련 정보