If text from file equal name function then run function
Clash 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?
linux bash scripting
add a comment |Â
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?
linux bash scripting
Yourfor 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
iswww.google.com
, then what do you expectecho 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
add a comment |Â
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?
linux bash scripting
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
linux bash scripting
edited Sep 5 at 21:56
Andy Dalton
4,8391520
4,8391520
asked Sep 5 at 21:12
unknown
305
305
Yourfor 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
iswww.google.com
, then what do you expectecho 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
add a comment |Â
Yourfor 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
iswww.google.com
, then what do you expectecho 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
add a comment |Â
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
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
add a comment |Â
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
.
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
answered Sep 5 at 22:22
ilkkachu
51.9k679144
51.9k679144
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
iswww.google.com
, then what do you expectecho 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