How to take blank value in the variable in shell script?Regular expression how to get value in shell...

How to pass query parameters in URL in Salesforce Summer 19 Release?

The origin of list data structure

Why did WWI include Japan?

Looking for sci-fi book based on Hinduism/Buddhism

Speed up this NIntegrate

Meaning of the (idiomatic?) expression "seghe mentali"

As black, how should one respond to 4. Qe2 by white in the Russian Game, Damiano Variation?

Sheared off exhasut pipe: How to fix without a welder?

Can my 2 children, aged 10 and 12, who are US citizens, travel to the USA on expired American passports?

How can Internet speed be 10 times slower without a router than when using the same connection with a router?

no sense/need/point

Piano: quaver triplets in RH v dotted quaver and semiquaver in LH

Hostile Divisor Numbers

Constitutional limitation of criminalizing behavior in US law?

Page count conversion from single to double-space for submissions

How to properly store the current value of int variable into a token list?

How do I allocate more memory to an app on Sheepshaver running Mac OS 9?

What happens if I accidentally leave an app running and click "Install Now" in Software Updater?

Is there a proof that the set of real numbers can exactly represent distances?

What is the closest airport to the center of the city it serves?

What's the 2-minute timer on mobile Deutsche Bahn tickets?

Does running exec do anything?

Determine if a grid contains another grid

Why would a military not separate its forces into different branches?



How to take blank value in the variable in shell script?


Regular expression how to get value in shell script?In a shell script, How to give a variable another value after one job is done?Shell script printing blank $USER variable when executed by SmartdShell Script to remove double quotes within the column valueHow to take snapshot of website through shell script?Take grep value and output the count and the value togetherIn shell script, how to give a variable the value that is shown on the screen?How to change the value of $SHELL variable?Printing variable value prints string Shell scriptingshell script with sentinel value -1






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







1















I have taken 2 variables in the grep command for search in the file. User is only putting one and pressing enter on the second variable, its throwing the error.



I want to set empty value on the variable second if user doesn't enter anything for the second one.



#!/bin/bash 
find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
echo enter the file name read file1
echo "enter varible 1"
read var1
echo "enter varible 2"
read var2
grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
grep -w $var1 output.txt | grep $var2 |sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt
paste -d' ' $file1 out2.txt > out3.txt
rm -rf output.txt
rm -rf $file1









share|improve this question
















bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Please include examples

    – Romeo Ninov
    Oct 13 '17 at 9:12






  • 1





    Please edit your question to include the relevant data and commands that you are using, along with any error messages or warnings that you may get.

    – Kusalananda
    Oct 13 '17 at 9:14











  • this is the script... if i dont put varible 2 value and simply press enter. it is through error

    – Rishabh Bahukhandi
    Oct 13 '17 at 9:20











  • The good practice is to add it in question, not as comment. Please check my edit

    – Romeo Ninov
    Oct 13 '17 at 9:21






  • 1





    By "does not enter anything" do you mean "only presses enter" or "doesn't even press enter"?

    – Kusalananda
    Oct 13 '17 at 9:33


















1















I have taken 2 variables in the grep command for search in the file. User is only putting one and pressing enter on the second variable, its throwing the error.



I want to set empty value on the variable second if user doesn't enter anything for the second one.



#!/bin/bash 
find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
echo enter the file name read file1
echo "enter varible 1"
read var1
echo "enter varible 2"
read var2
grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
grep -w $var1 output.txt | grep $var2 |sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt
paste -d' ' $file1 out2.txt > out3.txt
rm -rf output.txt
rm -rf $file1









share|improve this question
















bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Please include examples

    – Romeo Ninov
    Oct 13 '17 at 9:12






  • 1





    Please edit your question to include the relevant data and commands that you are using, along with any error messages or warnings that you may get.

    – Kusalananda
    Oct 13 '17 at 9:14











  • this is the script... if i dont put varible 2 value and simply press enter. it is through error

    – Rishabh Bahukhandi
    Oct 13 '17 at 9:20











  • The good practice is to add it in question, not as comment. Please check my edit

    – Romeo Ninov
    Oct 13 '17 at 9:21






  • 1





    By "does not enter anything" do you mean "only presses enter" or "doesn't even press enter"?

    – Kusalananda
    Oct 13 '17 at 9:33














1












1








1








I have taken 2 variables in the grep command for search in the file. User is only putting one and pressing enter on the second variable, its throwing the error.



I want to set empty value on the variable second if user doesn't enter anything for the second one.



#!/bin/bash 
find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
echo enter the file name read file1
echo "enter varible 1"
read var1
echo "enter varible 2"
read var2
grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
grep -w $var1 output.txt | grep $var2 |sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt
paste -d' ' $file1 out2.txt > out3.txt
rm -rf output.txt
rm -rf $file1









share|improve this question
















I have taken 2 variables in the grep command for search in the file. User is only putting one and pressing enter on the second variable, its throwing the error.



I want to set empty value on the variable second if user doesn't enter anything for the second one.



#!/bin/bash 
find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
echo enter the file name read file1
echo "enter varible 1"
read var1
echo "enter varible 2"
read var2
grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
grep -w $var1 output.txt | grep $var2 |sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt
paste -d' ' $file1 out2.txt > out3.txt
rm -rf output.txt
rm -rf $file1






shell-script shell grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 13 '17 at 9:27









Kusalananda

145k18273452




145k18273452










asked Oct 13 '17 at 9:06









Rishabh BahukhandiRishabh Bahukhandi

334




334





bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Please include examples

    – Romeo Ninov
    Oct 13 '17 at 9:12






  • 1





    Please edit your question to include the relevant data and commands that you are using, along with any error messages or warnings that you may get.

    – Kusalananda
    Oct 13 '17 at 9:14











  • this is the script... if i dont put varible 2 value and simply press enter. it is through error

    – Rishabh Bahukhandi
    Oct 13 '17 at 9:20











  • The good practice is to add it in question, not as comment. Please check my edit

    – Romeo Ninov
    Oct 13 '17 at 9:21






  • 1





    By "does not enter anything" do you mean "only presses enter" or "doesn't even press enter"?

    – Kusalananda
    Oct 13 '17 at 9:33



















  • Please include examples

    – Romeo Ninov
    Oct 13 '17 at 9:12






  • 1





    Please edit your question to include the relevant data and commands that you are using, along with any error messages or warnings that you may get.

    – Kusalananda
    Oct 13 '17 at 9:14











  • this is the script... if i dont put varible 2 value and simply press enter. it is through error

    – Rishabh Bahukhandi
    Oct 13 '17 at 9:20











  • The good practice is to add it in question, not as comment. Please check my edit

    – Romeo Ninov
    Oct 13 '17 at 9:21






  • 1





    By "does not enter anything" do you mean "only presses enter" or "doesn't even press enter"?

    – Kusalananda
    Oct 13 '17 at 9:33

















Please include examples

– Romeo Ninov
Oct 13 '17 at 9:12





Please include examples

– Romeo Ninov
Oct 13 '17 at 9:12




1




1





Please edit your question to include the relevant data and commands that you are using, along with any error messages or warnings that you may get.

– Kusalananda
Oct 13 '17 at 9:14





Please edit your question to include the relevant data and commands that you are using, along with any error messages or warnings that you may get.

– Kusalananda
Oct 13 '17 at 9:14













this is the script... if i dont put varible 2 value and simply press enter. it is through error

– Rishabh Bahukhandi
Oct 13 '17 at 9:20





this is the script... if i dont put varible 2 value and simply press enter. it is through error

– Rishabh Bahukhandi
Oct 13 '17 at 9:20













The good practice is to add it in question, not as comment. Please check my edit

– Romeo Ninov
Oct 13 '17 at 9:21





The good practice is to add it in question, not as comment. Please check my edit

– Romeo Ninov
Oct 13 '17 at 9:21




1




1





By "does not enter anything" do you mean "only presses enter" or "doesn't even press enter"?

– Kusalananda
Oct 13 '17 at 9:33





By "does not enter anything" do you mean "only presses enter" or "doesn't even press enter"?

– Kusalananda
Oct 13 '17 at 9:33










1 Answer
1






active

oldest

votes


















0














It seems like what you might be looking for is the ability to test for whether or not a variable was assigned a value. This can be done using Bash string-comparison operators. These operators are documented in several different common references, e.g.:




  • Bash Guide for Beginners: Chapter 7 - Conditional Statements


  • Advanced Bash-Scripting Guide: Section 7.3 - Other Comparison Operators



In particular, you can use the if [ -n "${string}" ] to test for non-null strings. Adapting your script to use this test might result in something like the following:



#!/bin/bash
find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
echo enter the file name read file1
echo "enter varible 1"
read var1
echo "enter varible 2"
read var2

if [[ -n "${var2}" ]]; then var2=""; fi

grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
grep -w $var1 output.txt | grep $var2 | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

paste -d' ' $file1 out2.txt > out3.txt
rm -rf output.txt
rm -rf $file1


That said, I don't think that will help solve your problem. To avoid your grep errors it should suffice to quote the variables, e.g. grep -w "${var1}" output.txt. Here is what your script looks like with quoted variables:



#!/bin/bash
find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
echo enter the file name read file1
echo "enter varible 1"
read var1
echo "enter varible 2"
read var2

if [[ -n "${var2}" ]];
var2=""
fi

grep -w "${var1}" output.txt | grep "${var2}" | cut -d' ' -f1,4,12,25 > $file1
grep -w "${var1}" output.txt | grep "${var2}" | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

paste -d' ' $file1 out2.txt > out3.txt
rm -rf output.txt
rm -rf $file1


An explanation for why you need to quote the variables in your grep expressions can be found in the Bash Guide for Beginners: Section 3.4.8 - Word Splitting:




The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. [...] Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.







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/3.0/"u003ecc by-sa 3.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%2f397886%2fhow-to-take-blank-value-in-the-variable-in-shell-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It seems like what you might be looking for is the ability to test for whether or not a variable was assigned a value. This can be done using Bash string-comparison operators. These operators are documented in several different common references, e.g.:




    • Bash Guide for Beginners: Chapter 7 - Conditional Statements


    • Advanced Bash-Scripting Guide: Section 7.3 - Other Comparison Operators



    In particular, you can use the if [ -n "${string}" ] to test for non-null strings. Adapting your script to use this test might result in something like the following:



    #!/bin/bash
    find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
    echo enter the file name read file1
    echo "enter varible 1"
    read var1
    echo "enter varible 2"
    read var2

    if [[ -n "${var2}" ]]; then var2=""; fi

    grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
    grep -w $var1 output.txt | grep $var2 | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

    paste -d' ' $file1 out2.txt > out3.txt
    rm -rf output.txt
    rm -rf $file1


    That said, I don't think that will help solve your problem. To avoid your grep errors it should suffice to quote the variables, e.g. grep -w "${var1}" output.txt. Here is what your script looks like with quoted variables:



    #!/bin/bash
    find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
    echo enter the file name read file1
    echo "enter varible 1"
    read var1
    echo "enter varible 2"
    read var2

    if [[ -n "${var2}" ]];
    var2=""
    fi

    grep -w "${var1}" output.txt | grep "${var2}" | cut -d' ' -f1,4,12,25 > $file1
    grep -w "${var1}" output.txt | grep "${var2}" | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

    paste -d' ' $file1 out2.txt > out3.txt
    rm -rf output.txt
    rm -rf $file1


    An explanation for why you need to quote the variables in your grep expressions can be found in the Bash Guide for Beginners: Section 3.4.8 - Word Splitting:




    The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. [...] Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.







    share|improve this answer






























      0














      It seems like what you might be looking for is the ability to test for whether or not a variable was assigned a value. This can be done using Bash string-comparison operators. These operators are documented in several different common references, e.g.:




      • Bash Guide for Beginners: Chapter 7 - Conditional Statements


      • Advanced Bash-Scripting Guide: Section 7.3 - Other Comparison Operators



      In particular, you can use the if [ -n "${string}" ] to test for non-null strings. Adapting your script to use this test might result in something like the following:



      #!/bin/bash
      find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
      echo enter the file name read file1
      echo "enter varible 1"
      read var1
      echo "enter varible 2"
      read var2

      if [[ -n "${var2}" ]]; then var2=""; fi

      grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
      grep -w $var1 output.txt | grep $var2 | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

      paste -d' ' $file1 out2.txt > out3.txt
      rm -rf output.txt
      rm -rf $file1


      That said, I don't think that will help solve your problem. To avoid your grep errors it should suffice to quote the variables, e.g. grep -w "${var1}" output.txt. Here is what your script looks like with quoted variables:



      #!/bin/bash
      find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
      echo enter the file name read file1
      echo "enter varible 1"
      read var1
      echo "enter varible 2"
      read var2

      if [[ -n "${var2}" ]];
      var2=""
      fi

      grep -w "${var1}" output.txt | grep "${var2}" | cut -d' ' -f1,4,12,25 > $file1
      grep -w "${var1}" output.txt | grep "${var2}" | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

      paste -d' ' $file1 out2.txt > out3.txt
      rm -rf output.txt
      rm -rf $file1


      An explanation for why you need to quote the variables in your grep expressions can be found in the Bash Guide for Beginners: Section 3.4.8 - Word Splitting:




      The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. [...] Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.







      share|improve this answer




























        0












        0








        0







        It seems like what you might be looking for is the ability to test for whether or not a variable was assigned a value. This can be done using Bash string-comparison operators. These operators are documented in several different common references, e.g.:




        • Bash Guide for Beginners: Chapter 7 - Conditional Statements


        • Advanced Bash-Scripting Guide: Section 7.3 - Other Comparison Operators



        In particular, you can use the if [ -n "${string}" ] to test for non-null strings. Adapting your script to use this test might result in something like the following:



        #!/bin/bash
        find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
        echo enter the file name read file1
        echo "enter varible 1"
        read var1
        echo "enter varible 2"
        read var2

        if [[ -n "${var2}" ]]; then var2=""; fi

        grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
        grep -w $var1 output.txt | grep $var2 | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

        paste -d' ' $file1 out2.txt > out3.txt
        rm -rf output.txt
        rm -rf $file1


        That said, I don't think that will help solve your problem. To avoid your grep errors it should suffice to quote the variables, e.g. grep -w "${var1}" output.txt. Here is what your script looks like with quoted variables:



        #!/bin/bash
        find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
        echo enter the file name read file1
        echo "enter varible 1"
        read var1
        echo "enter varible 2"
        read var2

        if [[ -n "${var2}" ]];
        var2=""
        fi

        grep -w "${var1}" output.txt | grep "${var2}" | cut -d' ' -f1,4,12,25 > $file1
        grep -w "${var1}" output.txt | grep "${var2}" | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

        paste -d' ' $file1 out2.txt > out3.txt
        rm -rf output.txt
        rm -rf $file1


        An explanation for why you need to quote the variables in your grep expressions can be found in the Bash Guide for Beginners: Section 3.4.8 - Word Splitting:




        The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. [...] Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.







        share|improve this answer















        It seems like what you might be looking for is the ability to test for whether or not a variable was assigned a value. This can be done using Bash string-comparison operators. These operators are documented in several different common references, e.g.:




        • Bash Guide for Beginners: Chapter 7 - Conditional Statements


        • Advanced Bash-Scripting Guide: Section 7.3 - Other Comparison Operators



        In particular, you can use the if [ -n "${string}" ] to test for non-null strings. Adapting your script to use this test might result in something like the following:



        #!/bin/bash
        find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
        echo enter the file name read file1
        echo "enter varible 1"
        read var1
        echo "enter varible 2"
        read var2

        if [[ -n "${var2}" ]]; then var2=""; fi

        grep -w $var1 output.txt | grep $var2 | cut -d' ' -f1,4,12,25 > $file1
        grep -w $var1 output.txt | grep $var2 | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

        paste -d' ' $file1 out2.txt > out3.txt
        rm -rf output.txt
        rm -rf $file1


        That said, I don't think that will help solve your problem. To avoid your grep errors it should suffice to quote the variables, e.g. grep -w "${var1}" output.txt. Here is what your script looks like with quoted variables:



        #!/bin/bash
        find /var/log/nginx/ -type f ( -iname "access.log*" ! -iname "access.log*.gz" ) -exec cat {} + >> output.txt
        echo enter the file name read file1
        echo "enter varible 1"
        read var1
        echo "enter varible 2"
        read var2

        if [[ -n "${var2}" ]];
        var2=""
        fi

        grep -w "${var1}" output.txt | grep "${var2}" | cut -d' ' -f1,4,12,25 > $file1
        grep -w "${var1}" output.txt | grep "${var2}" | sed 's#.*(aff_id=[[:digit:]]+).*(off_id=[[:digit:]]‌​+).*(trans_‌​id=[[:alnum:]]+)‌​;.*#1 2 3#' > out2.txt

        paste -d' ' $file1 out2.txt > out3.txt
        rm -rf output.txt
        rm -rf $file1


        An explanation for why you need to quote the variables in your grep expressions can be found in the Bash Guide for Beginners: Section 3.4.8 - Word Splitting:




        The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. [...] Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 16 '17 at 18:45

























        answered Oct 16 '17 at 18:22









        igaligal

        6,2511639




        6,2511639






























            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%2f397886%2fhow-to-take-blank-value-in-the-variable-in-shell-script%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

            Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

            The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

            Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...