cut -c funktioniert nicht bei meiner .sh-Datei

cut -c funktioniert nicht bei meiner .sh-Datei

Ich habe die folgenden Befehle ausprobiert

cut -c-11 ifshell.sh 

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

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

Aber jedes Mal erhalte ich den vollständigen Inhalt der .sh-Datei. Diese Befehle funktionieren perfekt bei .txt-Dateien. Das Hauptziel besteht darin, die ersten 11 Zeichen des Skripts „#!/bin/bash“ zu extrahieren, um zu prüfen, ob die Datei wirklich ein Bash-Bin-Skript ist.

Antwort1

Sie können auch den Standardbefehl verwenden 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

Antwort2

Möglicherweise ist für Ihr Anliegen das Folgende besser geeignet:

# #// 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 ;

Sie können dies auch mit dem Vorschlag von @netmonk mischen, wo ein Grep auf den Kopf prägnanter wäre, zB:

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

verwandte Informationen