简体   繁体   English

如何分析bash中一个txt文件的第一行

[英]How to analyse the first line of a txt file in bash

I need to check with my Bash script, if there is a Shebang in the frist line of a.txt file.我需要检查我的 Bash 脚本,如果 a.txt 文件的第一行中有 Shebang。 This is my code so far:到目前为止,这是我的代码:

#Lists all valid parameters#

listParameter(){

    echo "Invalid parameter. Please use one of the following keywords:"
    echo -e "(txxxxx | stat | all | plag)\n"
}
 

if [ -z $1 ]
then
    listParameter
    exit 1

else
    if [ ${1:0:1} == "t" ]
    then
        if [ ! -f "$1" ]
        then 
            echo -e "File missing. Please import $1 to this path.\n"
            exit 1
        else 
            FILE = $1
            FIRSTLINE="head -n 1 $FILE"
            if [FIRSTLINE == "#!"]
            then
                echo "Lorem Ipsum"
            else
                echo "Lorem Ipsum"
            fi
        fi

I need to make different "tests" by different command line parameters.我需要通过不同的命令行参数进行不同的“测试”。 So first of all I checked if §1 is starting with a t.所以首先我检查了§1是否以t开头。 Than I know its txxxxx parameter.比我知道它的 txxxxx 参数。 txxxxx is the name of a txt file. txxxx 是 txt 文件的名称。 Than I check if this file exist.比我检查这个文件是否存在。 If yes I want to check, if the first row of the file starts with a "#.".如果是,我想检查文件的第一行是否以“#.”开头。 But this is not working and i dont know why.但这不起作用,我不知道为什么。 The txxxxx file and the bash script are in the same folder, So all in all. txxxxx 文件和 bash 脚本在同一个文件夹中,总而言之。 I just dont know how I can open and analyse a certain.txt file..: :/我只是不知道如何打开和分析某个.txt文件..::/

Hope someone can help me.希望可以有人帮帮我。

It looks like you are not executing the "head -n 1..." command, but treating it as a string.看起来您没有执行“head -n 1 ...”命令,而是将其视为字符串。 To run it use:要运行它,请使用:

FIRSTLINE=$(head -n 1 "$FILE")

Then you probably want to check if first line starts with shebang, not necessarily is shebang.然后你可能想检查第一行是否以shebang开头,不一定是shebang。

You can use the builtin read .您可以使用内置的read You don't need to call an external command like head and invoke a subshell.您不需要调用像head这样的外部命令并调用子 shell。

#!/bin/bash

file=$1
if IFS= read -r firstline < "$file" && [[ $firstline = '#!'* ]]
then
    printf 'The file %s contains shebang\n' "$file"
fi

An aside note: By convention, you shouldn't use capitalized variable names for your own variables in shell programming.附注:按照惯例,在 shell 编程中,您不应该为自己的变量使用大写的变量名。 Environment variables and internal shell variables are capitalized.环境变量和内部 shell 变量大写。 All other variable names should be lower case.所有其他变量名称应为小写。 This convention avoids accidentally overriding environmental and internal variables.此约定避免意外覆盖环境和内部变量。

Whitespace matters.空白很重要。 The line if [FIRSTLINE == "#!"] is incorrect, as it attempts to executes a command names [FIRSTLINE , which probably does not exist. if [FIRSTLINE == "#!"]行不正确,因为它尝试执行名称为[FIRSTLINE的命令,该命令可能不存在。 You want:你要:

 if [ "$FIRSTLINE" = "#!" ]

Also, instead of if [ -z $1 ] , you ought to use if [ -z "$1" ]此外,您应该使用if [ -z "$1" ]而不是if [ -z $1 ] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM