If text from file equal name function then run function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












File URLs.txt:



$ cat URLs.txt
www.google.com >/path/to/save


That's my script:



dl_url() xargs
done
done < ./URLs.txt
done


google.com()
while true; do
echo "blah blah blah"
done



It's only printing the URLs hostname not start proper function this should start another function depends on URL like google.com start google.com function, how change that?










share|improve this question























  • Your for i in $website_url doesn't make sense to me. At that point, $website_url will be only one URL -- the one that was read at the top of the containing loop.
    – Andy Dalton
    Sep 5 at 22:00






  • 1




    Also, if $website_url is www.google.com, then what do you expect echo www.google.com | awk -F '/.' 'print $2' to print?
    – Andy Dalton
    Sep 5 at 22:01










  • i use that script for i in $website_url from something else and its not working i need only script to run if URL from file are equal to function name like google.com function and run that function google.com
    – unknown
    Sep 5 at 22:07











  • or other way run function depends on what url are in that file
    – unknown
    Sep 5 at 22:11














up vote
1
down vote

favorite












File URLs.txt:



$ cat URLs.txt
www.google.com >/path/to/save


That's my script:



dl_url() xargs
done
done < ./URLs.txt
done


google.com()
while true; do
echo "blah blah blah"
done



It's only printing the URLs hostname not start proper function this should start another function depends on URL like google.com start google.com function, how change that?










share|improve this question























  • Your for i in $website_url doesn't make sense to me. At that point, $website_url will be only one URL -- the one that was read at the top of the containing loop.
    – Andy Dalton
    Sep 5 at 22:00






  • 1




    Also, if $website_url is www.google.com, then what do you expect echo www.google.com | awk -F '/.' 'print $2' to print?
    – Andy Dalton
    Sep 5 at 22:01










  • i use that script for i in $website_url from something else and its not working i need only script to run if URL from file are equal to function name like google.com function and run that function google.com
    – unknown
    Sep 5 at 22:07











  • or other way run function depends on what url are in that file
    – unknown
    Sep 5 at 22:11












up vote
1
down vote

favorite









up vote
1
down vote

favorite











File URLs.txt:



$ cat URLs.txt
www.google.com >/path/to/save


That's my script:



dl_url() xargs
done
done < ./URLs.txt
done


google.com()
while true; do
echo "blah blah blah"
done



It's only printing the URLs hostname not start proper function this should start another function depends on URL like google.com start google.com function, how change that?










share|improve this question















File URLs.txt:



$ cat URLs.txt
www.google.com >/path/to/save


That's my script:



dl_url() xargs
done
done < ./URLs.txt
done


google.com()
while true; do
echo "blah blah blah"
done



It's only printing the URLs hostname not start proper function this should start another function depends on URL like google.com start google.com function, how change that?







linux bash scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 at 21:56









Andy Dalton

4,8391520




4,8391520










asked Sep 5 at 21:12









unknown

305




305











  • Your for i in $website_url doesn't make sense to me. At that point, $website_url will be only one URL -- the one that was read at the top of the containing loop.
    – Andy Dalton
    Sep 5 at 22:00






  • 1




    Also, if $website_url is www.google.com, then what do you expect echo www.google.com | awk -F '/.' 'print $2' to print?
    – Andy Dalton
    Sep 5 at 22:01










  • i use that script for i in $website_url from something else and its not working i need only script to run if URL from file are equal to function name like google.com function and run that function google.com
    – unknown
    Sep 5 at 22:07











  • or other way run function depends on what url are in that file
    – unknown
    Sep 5 at 22:11
















  • Your for i in $website_url doesn't make sense to me. At that point, $website_url will be only one URL -- the one that was read at the top of the containing loop.
    – Andy Dalton
    Sep 5 at 22:00






  • 1




    Also, if $website_url is www.google.com, then what do you expect echo www.google.com | awk -F '/.' 'print $2' to print?
    – Andy Dalton
    Sep 5 at 22:01










  • i use that script for i in $website_url from something else and its not working i need only script to run if URL from file are equal to function name like google.com function and run that function google.com
    – unknown
    Sep 5 at 22:07











  • or other way run function depends on what url are in that file
    – unknown
    Sep 5 at 22:11















Your for i in $website_url doesn't make sense to me. At that point, $website_url will be only one URL -- the one that was read at the top of the containing loop.
– Andy Dalton
Sep 5 at 22:00




Your for i in $website_url doesn't make sense to me. At that point, $website_url will be only one URL -- the one that was read at the top of the containing loop.
– Andy Dalton
Sep 5 at 22:00




1




1




Also, if $website_url is www.google.com, then what do you expect echo www.google.com | awk -F '/.' 'print $2' to print?
– Andy Dalton
Sep 5 at 22:01




Also, if $website_url is www.google.com, then what do you expect echo www.google.com | awk -F '/.' 'print $2' to print?
– Andy Dalton
Sep 5 at 22:01












i use that script for i in $website_url from something else and its not working i need only script to run if URL from file are equal to function name like google.com function and run that function google.com
– unknown
Sep 5 at 22:07





i use that script for i in $website_url from something else and its not working i need only script to run if URL from file are equal to function name like google.com function and run that function google.com
– unknown
Sep 5 at 22:07













or other way run function depends on what url are in that file
– unknown
Sep 5 at 22:11




or other way run function depends on what url are in that file
– unknown
Sep 5 at 22:11










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Not sure why you have all those infinitely repeating while loops in there. As Andy pointed out your awk statement is strange and likely not going to do what you want it to. The below script should work better for your purposes.



#!/bin/bash
cleanup()
if [[ $1 == "www.google.com" ]]; then
echo "blah blah blah"
else
echo "url not found"
fi


while IFS='>' read -r website_url gallery_dir; do

echo "Downloading $website_url"
echo "To $gallery_dir"; sleep 1
mkdir -p ./update_photo_temp/
wget --quiet $website_url -P ./update_photo_temp/

for i in $website_url; do
cleanup $i
done

done < ./URLs.txt





share|improve this answer




















  • i have many and big functions for each urls many lines of code
    – unknown
    Sep 5 at 22:16










  • yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
    – unknown
    Sep 5 at 22:30










  • i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
    – unknown
    Sep 5 at 22:34

















up vote
2
down vote













To answer the question in the title, as I understood it: You want to read lines of input, and for each that is equal to the name of a shell function, run that function.



You can use declare -f func to see if func is defined, so something like this:



#!/bin/bash

foo() echo foofoo;
bar() echo barbar;
while read funcname; do
if declare -f "$funcname" &> /dev/null; then
"$funcname"
fi
done


Note that in your code:



IFS='>' read -r website_url gallery_dir


leaves the space before > in website_url, since the IFS you set here doesn't contain any whitespace. You could use IFS='> ' read ... instead.



for i in $website_url; do
echo $i | awk -F '/.' 'print $2' | xargs
done


I'm not sure what this is supposed to do. for i in $website_url splits and globs the value of the variable, and has i loop over the values. The xargs at the end of the pipeline echoes whatever it gets as input. None of that runs any functions. Also, the awk field separator /. matches a slash followed by any character, so if the input was e.g. foo/bar, the resulting output would be ar.






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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f467139%2fif-text-from-file-equal-name-function-then-run-function%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Not sure why you have all those infinitely repeating while loops in there. As Andy pointed out your awk statement is strange and likely not going to do what you want it to. The below script should work better for your purposes.



    #!/bin/bash
    cleanup()
    if [[ $1 == "www.google.com" ]]; then
    echo "blah blah blah"
    else
    echo "url not found"
    fi


    while IFS='>' read -r website_url gallery_dir; do

    echo "Downloading $website_url"
    echo "To $gallery_dir"; sleep 1
    mkdir -p ./update_photo_temp/
    wget --quiet $website_url -P ./update_photo_temp/

    for i in $website_url; do
    cleanup $i
    done

    done < ./URLs.txt





    share|improve this answer




















    • i have many and big functions for each urls many lines of code
      – unknown
      Sep 5 at 22:16










    • yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
      – unknown
      Sep 5 at 22:30










    • i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
      – unknown
      Sep 5 at 22:34














    up vote
    1
    down vote



    accepted










    Not sure why you have all those infinitely repeating while loops in there. As Andy pointed out your awk statement is strange and likely not going to do what you want it to. The below script should work better for your purposes.



    #!/bin/bash
    cleanup()
    if [[ $1 == "www.google.com" ]]; then
    echo "blah blah blah"
    else
    echo "url not found"
    fi


    while IFS='>' read -r website_url gallery_dir; do

    echo "Downloading $website_url"
    echo "To $gallery_dir"; sleep 1
    mkdir -p ./update_photo_temp/
    wget --quiet $website_url -P ./update_photo_temp/

    for i in $website_url; do
    cleanup $i
    done

    done < ./URLs.txt





    share|improve this answer




















    • i have many and big functions for each urls many lines of code
      – unknown
      Sep 5 at 22:16










    • yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
      – unknown
      Sep 5 at 22:30










    • i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
      – unknown
      Sep 5 at 22:34












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Not sure why you have all those infinitely repeating while loops in there. As Andy pointed out your awk statement is strange and likely not going to do what you want it to. The below script should work better for your purposes.



    #!/bin/bash
    cleanup()
    if [[ $1 == "www.google.com" ]]; then
    echo "blah blah blah"
    else
    echo "url not found"
    fi


    while IFS='>' read -r website_url gallery_dir; do

    echo "Downloading $website_url"
    echo "To $gallery_dir"; sleep 1
    mkdir -p ./update_photo_temp/
    wget --quiet $website_url -P ./update_photo_temp/

    for i in $website_url; do
    cleanup $i
    done

    done < ./URLs.txt





    share|improve this answer












    Not sure why you have all those infinitely repeating while loops in there. As Andy pointed out your awk statement is strange and likely not going to do what you want it to. The below script should work better for your purposes.



    #!/bin/bash
    cleanup()
    if [[ $1 == "www.google.com" ]]; then
    echo "blah blah blah"
    else
    echo "url not found"
    fi


    while IFS='>' read -r website_url gallery_dir; do

    echo "Downloading $website_url"
    echo "To $gallery_dir"; sleep 1
    mkdir -p ./update_photo_temp/
    wget --quiet $website_url -P ./update_photo_temp/

    for i in $website_url; do
    cleanup $i
    done

    done < ./URLs.txt






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 5 at 22:11









    hhoke1

    35917




    35917











    • i have many and big functions for each urls many lines of code
      – unknown
      Sep 5 at 22:16










    • yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
      – unknown
      Sep 5 at 22:30










    • i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
      – unknown
      Sep 5 at 22:34
















    • i have many and big functions for each urls many lines of code
      – unknown
      Sep 5 at 22:16










    • yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
      – unknown
      Sep 5 at 22:30










    • i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
      – unknown
      Sep 5 at 22:34















    i have many and big functions for each urls many lines of code
    – unknown
    Sep 5 at 22:16




    i have many and big functions for each urls many lines of code
    – unknown
    Sep 5 at 22:16












    yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
    – unknown
    Sep 5 at 22:30




    yes thats script looks much better i can use it but how to cut only the google.com part from url file? i mean whole url is longer like www.google.com/blah/bla/bla, should be there awk command to print only google.com not whole url then run your script
    – unknown
    Sep 5 at 22:30












    i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
    – unknown
    Sep 5 at 22:34




    i think im gonna use new variable like hostname= awk command to print only host not all url and run this script. Thank you all!
    – unknown
    Sep 5 at 22:34












    up vote
    2
    down vote













    To answer the question in the title, as I understood it: You want to read lines of input, and for each that is equal to the name of a shell function, run that function.



    You can use declare -f func to see if func is defined, so something like this:



    #!/bin/bash

    foo() echo foofoo;
    bar() echo barbar;
    while read funcname; do
    if declare -f "$funcname" &> /dev/null; then
    "$funcname"
    fi
    done


    Note that in your code:



    IFS='>' read -r website_url gallery_dir


    leaves the space before > in website_url, since the IFS you set here doesn't contain any whitespace. You could use IFS='> ' read ... instead.



    for i in $website_url; do
    echo $i | awk -F '/.' 'print $2' | xargs
    done


    I'm not sure what this is supposed to do. for i in $website_url splits and globs the value of the variable, and has i loop over the values. The xargs at the end of the pipeline echoes whatever it gets as input. None of that runs any functions. Also, the awk field separator /. matches a slash followed by any character, so if the input was e.g. foo/bar, the resulting output would be ar.






    share|improve this answer
























      up vote
      2
      down vote













      To answer the question in the title, as I understood it: You want to read lines of input, and for each that is equal to the name of a shell function, run that function.



      You can use declare -f func to see if func is defined, so something like this:



      #!/bin/bash

      foo() echo foofoo;
      bar() echo barbar;
      while read funcname; do
      if declare -f "$funcname" &> /dev/null; then
      "$funcname"
      fi
      done


      Note that in your code:



      IFS='>' read -r website_url gallery_dir


      leaves the space before > in website_url, since the IFS you set here doesn't contain any whitespace. You could use IFS='> ' read ... instead.



      for i in $website_url; do
      echo $i | awk -F '/.' 'print $2' | xargs
      done


      I'm not sure what this is supposed to do. for i in $website_url splits and globs the value of the variable, and has i loop over the values. The xargs at the end of the pipeline echoes whatever it gets as input. None of that runs any functions. Also, the awk field separator /. matches a slash followed by any character, so if the input was e.g. foo/bar, the resulting output would be ar.






      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        To answer the question in the title, as I understood it: You want to read lines of input, and for each that is equal to the name of a shell function, run that function.



        You can use declare -f func to see if func is defined, so something like this:



        #!/bin/bash

        foo() echo foofoo;
        bar() echo barbar;
        while read funcname; do
        if declare -f "$funcname" &> /dev/null; then
        "$funcname"
        fi
        done


        Note that in your code:



        IFS='>' read -r website_url gallery_dir


        leaves the space before > in website_url, since the IFS you set here doesn't contain any whitespace. You could use IFS='> ' read ... instead.



        for i in $website_url; do
        echo $i | awk -F '/.' 'print $2' | xargs
        done


        I'm not sure what this is supposed to do. for i in $website_url splits and globs the value of the variable, and has i loop over the values. The xargs at the end of the pipeline echoes whatever it gets as input. None of that runs any functions. Also, the awk field separator /. matches a slash followed by any character, so if the input was e.g. foo/bar, the resulting output would be ar.






        share|improve this answer












        To answer the question in the title, as I understood it: You want to read lines of input, and for each that is equal to the name of a shell function, run that function.



        You can use declare -f func to see if func is defined, so something like this:



        #!/bin/bash

        foo() echo foofoo;
        bar() echo barbar;
        while read funcname; do
        if declare -f "$funcname" &> /dev/null; then
        "$funcname"
        fi
        done


        Note that in your code:



        IFS='>' read -r website_url gallery_dir


        leaves the space before > in website_url, since the IFS you set here doesn't contain any whitespace. You could use IFS='> ' read ... instead.



        for i in $website_url; do
        echo $i | awk -F '/.' 'print $2' | xargs
        done


        I'm not sure what this is supposed to do. for i in $website_url splits and globs the value of the variable, and has i loop over the values. The xargs at the end of the pipeline echoes whatever it gets as input. None of that runs any functions. Also, the awk field separator /. matches a slash followed by any character, so if the input was e.g. foo/bar, the resulting output would be ar.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 5 at 22:22









        ilkkachu

        51.9k679144




        51.9k679144



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467139%2fif-text-from-file-equal-name-function-then-run-function%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?