cut -c가 내 .sh 파일에서 작동하지 않습니다.

cut -c가 내 .sh 파일에서 작동하지 않습니다.

나는 다음 명령을 시도했습니다

cut -c-11 ifshell.sh 

cat ifshell.sh | cut -c-11 ifshell.sh 

cat ifshell.sh | awk '{print $1} | cut -c-11 ifshell.sh

하지만 매번 .sh 파일의 전체 내용을 얻을 때마다. 이 명령은 .txt 파일에서 완벽하게 작동합니다. 주요 목표는 파일이 실제로 bash bin 스크립트인지 확인하기 위해 "#!/bin/bash" 스크립트의 처음 11자를 추출하는 것입니다.

답변1

표준 ̀ file명령을 사용할 수도 있습니다.

[PRD][]user@localhost:~ 17:21:30
$ head -n 1 setproxymkt.sh 
#!/bin/bash
[PRD][]user@localhost:~ 17:21:38
$ file setproxymkt.sh 
setproxymkt.sh: Bourne-Again shell script, ASCII text executable

답변2

달성하려는 작업에는 다음이 더 적합할 수 있습니다.

# #// FILE could be a for-loop as well for example.
FILE="bash_scropt.sh" ;
if grep '#!/bin/bash' $FILE 1>/dev/null ; then
    printf "$FILE bash-script\n" ;
else
    printf "> $FILE -- NOT bash\n" ;
fi ;

머리에 grep이 더 간결해지는 @netmonk 제안과 이것을 혼합할 수도 있습니다. 예:

FILE="bash_scropt.sh" ; if head -n 1 $FILE | grep '#!/bin/bash' 1>/dev/null ; then printf "$FILE bash-script\n" ; else printf "> $FILE -- NOT bash\n" ; fi

관련 정보