Using If, then…fi command how to determine if user has input correct file format?bash: how do you return...

Principled construction of the quaternions

Missing quartile in boxplot

Bothered by watching coworkers slacking off

Are there types of animals that can't make the trip to space? (physiologically)

How to interpret the challenge rating of creatures?

Citing CPLEX 12.9

What is the point of impeaching Trump?

How to say "respectively" in German when listing (enumerating) things

Why the first octet of a MAC address always end with a binary 0?

How to identify whether a publisher is genuine or not?

SOQL injection vulnerability issue

PhD Length: are shorter PhD degrees (from different countries) valued differently in other counter countries where PhD Is a longer process?

Manager told a colleague of mine I was getting fired soon

How is this situation not a checkmate?

Why do personal finance apps focus on outgoings rather than income

What is the idiomatic solution in SQL Server for reserving a block of ids for use in a bulk insert?

Does the US Armed Forces refuse to recruit anyone with an IQ less than 83?

Can an untrusted VPN client monitor my network activity?

The answer is a girl's name (my future granddaughter) - can anyone help?

Giving a good fancy look to a simple table

Can I bring this power bank on board the aircraft?

How can Germany increase investments in Russia while EU economic sanctions against Russia are still in place?

Decision Variable Value from a Set (Gurobi)

Do jackscrews suffer from blowdown?



Using If, then…fi command how to determine if user has input correct file format?


bash: how do you return file extensions?Use saved input for scriptsDynamically write bash script from another?How to write bash script while using command as condition in if statement?I want to add a variable for the results from the formula of one variable and the beginning of another variableStorage disk info - Replace multiple Input values to output filePassing comma separated variable to remote SSH Session






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}








0















I am trying to create an extremely basic bash script that tells a user if they input the correct file format for the script before continuing. For example:



echo "Input .txt file here:"
read file
if $file = *.txt
then
echo "File is in correct format."
else
echo "File is not in correct format. Please recheck your file."
fi


What can be used to do this?



The command (below) does not work and just reports that the 'else' rather than do what I truly want.



$file = *.txt









share|improve this question

























  • Possible duplicate of bash: how do you return file extensions?

    – muru
    5 mins ago


















0















I am trying to create an extremely basic bash script that tells a user if they input the correct file format for the script before continuing. For example:



echo "Input .txt file here:"
read file
if $file = *.txt
then
echo "File is in correct format."
else
echo "File is not in correct format. Please recheck your file."
fi


What can be used to do this?



The command (below) does not work and just reports that the 'else' rather than do what I truly want.



$file = *.txt









share|improve this question

























  • Possible duplicate of bash: how do you return file extensions?

    – muru
    5 mins ago














0












0








0








I am trying to create an extremely basic bash script that tells a user if they input the correct file format for the script before continuing. For example:



echo "Input .txt file here:"
read file
if $file = *.txt
then
echo "File is in correct format."
else
echo "File is not in correct format. Please recheck your file."
fi


What can be used to do this?



The command (below) does not work and just reports that the 'else' rather than do what I truly want.



$file = *.txt









share|improve this question














I am trying to create an extremely basic bash script that tells a user if they input the correct file format for the script before continuing. For example:



echo "Input .txt file here:"
read file
if $file = *.txt
then
echo "File is in correct format."
else
echo "File is not in correct format. Please recheck your file."
fi


What can be used to do this?



The command (below) does not work and just reports that the 'else' rather than do what I truly want.



$file = *.txt






linux shell-script






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 34 mins ago









ryannellieryannellie

32 bronze badges




32 bronze badges
















  • Possible duplicate of bash: how do you return file extensions?

    – muru
    5 mins ago



















  • Possible duplicate of bash: how do you return file extensions?

    – muru
    5 mins ago

















Possible duplicate of bash: how do you return file extensions?

– muru
5 mins ago





Possible duplicate of bash: how do you return file extensions?

– muru
5 mins ago










2 Answers
2






active

oldest

votes


















0
















You can try the below script :



if [ .$(echo $file| cut -d. -f2) == .txt ]
then
echo "File is in correct format."
else
echo "File is not in correct format. Please recheck your file."
fi




share


























  • Thank you! This worked perfectly.

    – ryannellie
    2 mins ago



















0
















Of course, just a file extension is not a file format. It's just text that anyone can change with a simple:



mv image.png word.txt


That being said, in this case, you can simply use the basename command:



base=`basename "$file" .txt`
if test "$base" != "$file"
then
echo "Success!"
else
echo "Wrong extension..."
fi





share|improve this answer



























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });















    draft saved

    draft discarded
















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f543768%2fusing-if-then-fi-command-how-to-determine-if-user-has-input-correct-file-form%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    You can try the below script :



    if [ .$(echo $file| cut -d. -f2) == .txt ]
    then
    echo "File is in correct format."
    else
    echo "File is not in correct format. Please recheck your file."
    fi




    share


























    • Thank you! This worked perfectly.

      – ryannellie
      2 mins ago
















    0
















    You can try the below script :



    if [ .$(echo $file| cut -d. -f2) == .txt ]
    then
    echo "File is in correct format."
    else
    echo "File is not in correct format. Please recheck your file."
    fi




    share


























    • Thank you! This worked perfectly.

      – ryannellie
      2 mins ago














    0














    0










    0









    You can try the below script :



    if [ .$(echo $file| cut -d. -f2) == .txt ]
    then
    echo "File is in correct format."
    else
    echo "File is not in correct format. Please recheck your file."
    fi




    share













    You can try the below script :



    if [ .$(echo $file| cut -d. -f2) == .txt ]
    then
    echo "File is in correct format."
    else
    echo "File is not in correct format. Please recheck your file."
    fi





    share











    share


    share










    answered 8 mins ago









    PacifistPacifist

    3,4821 gold badge6 silver badges13 bronze badges




    3,4821 gold badge6 silver badges13 bronze badges
















    • Thank you! This worked perfectly.

      – ryannellie
      2 mins ago



















    • Thank you! This worked perfectly.

      – ryannellie
      2 mins ago

















    Thank you! This worked perfectly.

    – ryannellie
    2 mins ago





    Thank you! This worked perfectly.

    – ryannellie
    2 mins ago













    0
















    Of course, just a file extension is not a file format. It's just text that anyone can change with a simple:



    mv image.png word.txt


    That being said, in this case, you can simply use the basename command:



    base=`basename "$file" .txt`
    if test "$base" != "$file"
    then
    echo "Success!"
    else
    echo "Wrong extension..."
    fi





    share|improve this answer






























      0
















      Of course, just a file extension is not a file format. It's just text that anyone can change with a simple:



      mv image.png word.txt


      That being said, in this case, you can simply use the basename command:



      base=`basename "$file" .txt`
      if test "$base" != "$file"
      then
      echo "Success!"
      else
      echo "Wrong extension..."
      fi





      share|improve this answer




























        0














        0










        0









        Of course, just a file extension is not a file format. It's just text that anyone can change with a simple:



        mv image.png word.txt


        That being said, in this case, you can simply use the basename command:



        base=`basename "$file" .txt`
        if test "$base" != "$file"
        then
        echo "Success!"
        else
        echo "Wrong extension..."
        fi





        share|improve this answer













        Of course, just a file extension is not a file format. It's just text that anyone can change with a simple:



        mv image.png word.txt


        That being said, in this case, you can simply use the basename command:



        base=`basename "$file" .txt`
        if test "$base" != "$file"
        then
        echo "Success!"
        else
        echo "Wrong extension..."
        fi






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 27 mins ago









        Alexis WilkeAlexis Wilke

        1,1307 silver badges18 bronze badges




        1,1307 silver badges18 bronze badges


































            draft saved

            draft discarded



















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f543768%2fusing-if-then-fi-command-how-to-determine-if-user-has-input-correct-file-form%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m