Using Multiple Function to get an output in a single Line
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am trying to create output which keeps hostname,ssh protocol and root login information with bash script.
And I would like to do it with developing functions. I developed .sh but it does not work.
Where is the problem in this sh?
Server Version Red Hat 7
my expected output should be;
xyz|hostname|Protocol X|Root Access Denied
And I would like to start to output "xyz" in order to pars my out but.
#!/bin/bash
host()
local tmpfile=$(mktemp)
hostname > "$tmpfile"
printf '%s' "$tmpfile"
protocol() grep Protocol
rootlogin() grep -i "PermitRootLogin yes"
}
tmpfile=$( host )
host "$tmpfile"
protocol "$tmpfile"
rootlogin "$tmpfile"
> fonk.out
rm -f "$tmpfile"
bash shell rhel function bash-functions
add a comment |Â
up vote
0
down vote
favorite
I am trying to create output which keeps hostname,ssh protocol and root login information with bash script.
And I would like to do it with developing functions. I developed .sh but it does not work.
Where is the problem in this sh?
Server Version Red Hat 7
my expected output should be;
xyz|hostname|Protocol X|Root Access Denied
And I would like to start to output "xyz" in order to pars my out but.
#!/bin/bash
host()
local tmpfile=$(mktemp)
hostname > "$tmpfile"
printf '%s' "$tmpfile"
protocol() grep Protocol
rootlogin() grep -i "PermitRootLogin yes"
}
tmpfile=$( host )
host "$tmpfile"
protocol "$tmpfile"
rootlogin "$tmpfile"
> fonk.out
rm -f "$tmpfile"
bash shell rhel function bash-functions
You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends inProtocol}
needs to look either likeProtocol; }
or the}
should go on the next line. You also do some unnecessary grepping to test ifPermitRootLogin
is set (you could anchor the pattern to the start with^PermitRootLogin yes
). But the main thing looks like a syntax error typo.
â Kusalananda
Aug 15 at 6:29
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to create output which keeps hostname,ssh protocol and root login information with bash script.
And I would like to do it with developing functions. I developed .sh but it does not work.
Where is the problem in this sh?
Server Version Red Hat 7
my expected output should be;
xyz|hostname|Protocol X|Root Access Denied
And I would like to start to output "xyz" in order to pars my out but.
#!/bin/bash
host()
local tmpfile=$(mktemp)
hostname > "$tmpfile"
printf '%s' "$tmpfile"
protocol() grep Protocol
rootlogin() grep -i "PermitRootLogin yes"
}
tmpfile=$( host )
host "$tmpfile"
protocol "$tmpfile"
rootlogin "$tmpfile"
> fonk.out
rm -f "$tmpfile"
bash shell rhel function bash-functions
I am trying to create output which keeps hostname,ssh protocol and root login information with bash script.
And I would like to do it with developing functions. I developed .sh but it does not work.
Where is the problem in this sh?
Server Version Red Hat 7
my expected output should be;
xyz|hostname|Protocol X|Root Access Denied
And I would like to start to output "xyz" in order to pars my out but.
#!/bin/bash
host()
local tmpfile=$(mktemp)
hostname > "$tmpfile"
printf '%s' "$tmpfile"
protocol() grep Protocol
rootlogin() grep -i "PermitRootLogin yes"
}
tmpfile=$( host )
host "$tmpfile"
protocol "$tmpfile"
rootlogin "$tmpfile"
> fonk.out
rm -f "$tmpfile"
bash shell rhel function bash-functions
bash shell rhel function bash-functions
edited Sep 7 at 10:05
Rui F Ribeiro
36.6k1271116
36.6k1271116
asked Aug 15 at 6:19
1010111100011
236
236
You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends inProtocol}
needs to look either likeProtocol; }
or the}
should go on the next line. You also do some unnecessary grepping to test ifPermitRootLogin
is set (you could anchor the pattern to the start with^PermitRootLogin yes
). But the main thing looks like a syntax error typo.
â Kusalananda
Aug 15 at 6:29
add a comment |Â
You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends inProtocol}
needs to look either likeProtocol; }
or the}
should go on the next line. You also do some unnecessary grepping to test ifPermitRootLogin
is set (you could anchor the pattern to the start with^PermitRootLogin yes
). But the main thing looks like a syntax error typo.
â Kusalananda
Aug 15 at 6:29
You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends in
Protocol}
needs to look either like Protocol; }
or the }
should go on the next line. You also do some unnecessary grepping to test if PermitRootLogin
is set (you could anchor the pattern to the start with ^PermitRootLogin yes
). But the main thing looks like a syntax error typo.â Kusalananda
Aug 15 at 6:29
You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends in
Protocol}
needs to look either like Protocol; }
or the }
should go on the next line. You also do some unnecessary grepping to test if PermitRootLogin
is set (you could anchor the pattern to the start with ^PermitRootLogin yes
). But the main thing looks like a syntax error typo.â Kusalananda
Aug 15 at 6:29
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The poor indentation and layout of your script muddles the question, but the basic answer is
printf '%s|%s|%s|%sn' "$(field1)" "$(field2)" "$(field3") "$(field4)"
Refactored into this, and with indentation etc cleaned up, your script becomes
#!/bin/bash
host()
hostname
protocol()
# avoid useless use of cat; get rid of unused parameter
# ... do you need sudo here too?
grep Protocol /etc/ssh/sshd_config
rootlogin()
# straighten out massive spaghetti pretzel; remove unused parameter
# ... can you avoid sudo cat here?
if sudo cat /etc/ssh/sshd_config
printf '%s|%s|%s|%sn' "$1" "$(host)" "$(protocol)" "$(rootlogin)" >fonk.out
The last line is somewhat speculative; your current script doesn't seem to print the first field at all, and it's not clear what it's supposed to contain.
This no longer uses a temporary file, but one antipattern in your attempt was to create the temp file in an unrelated function. When you really do need a temp file, it would probably be a good idea to create it separately, then use it as a parameter everywhere. Like
tmp=$(mktemp) || exit
# arrange for temp file to be removed in case of errors or signals, too
trap 'rm -f "$tmp"' EXIT
trap 'exit' ERROR HUP QUIT TRAP TERM
function1 "$tmp"
function2 "$tmp"
: etc
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The poor indentation and layout of your script muddles the question, but the basic answer is
printf '%s|%s|%s|%sn' "$(field1)" "$(field2)" "$(field3") "$(field4)"
Refactored into this, and with indentation etc cleaned up, your script becomes
#!/bin/bash
host()
hostname
protocol()
# avoid useless use of cat; get rid of unused parameter
# ... do you need sudo here too?
grep Protocol /etc/ssh/sshd_config
rootlogin()
# straighten out massive spaghetti pretzel; remove unused parameter
# ... can you avoid sudo cat here?
if sudo cat /etc/ssh/sshd_config
printf '%s|%s|%s|%sn' "$1" "$(host)" "$(protocol)" "$(rootlogin)" >fonk.out
The last line is somewhat speculative; your current script doesn't seem to print the first field at all, and it's not clear what it's supposed to contain.
This no longer uses a temporary file, but one antipattern in your attempt was to create the temp file in an unrelated function. When you really do need a temp file, it would probably be a good idea to create it separately, then use it as a parameter everywhere. Like
tmp=$(mktemp) || exit
# arrange for temp file to be removed in case of errors or signals, too
trap 'rm -f "$tmp"' EXIT
trap 'exit' ERROR HUP QUIT TRAP TERM
function1 "$tmp"
function2 "$tmp"
: etc
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
add a comment |Â
up vote
0
down vote
accepted
The poor indentation and layout of your script muddles the question, but the basic answer is
printf '%s|%s|%s|%sn' "$(field1)" "$(field2)" "$(field3") "$(field4)"
Refactored into this, and with indentation etc cleaned up, your script becomes
#!/bin/bash
host()
hostname
protocol()
# avoid useless use of cat; get rid of unused parameter
# ... do you need sudo here too?
grep Protocol /etc/ssh/sshd_config
rootlogin()
# straighten out massive spaghetti pretzel; remove unused parameter
# ... can you avoid sudo cat here?
if sudo cat /etc/ssh/sshd_config
printf '%s|%s|%s|%sn' "$1" "$(host)" "$(protocol)" "$(rootlogin)" >fonk.out
The last line is somewhat speculative; your current script doesn't seem to print the first field at all, and it's not clear what it's supposed to contain.
This no longer uses a temporary file, but one antipattern in your attempt was to create the temp file in an unrelated function. When you really do need a temp file, it would probably be a good idea to create it separately, then use it as a parameter everywhere. Like
tmp=$(mktemp) || exit
# arrange for temp file to be removed in case of errors or signals, too
trap 'rm -f "$tmp"' EXIT
trap 'exit' ERROR HUP QUIT TRAP TERM
function1 "$tmp"
function2 "$tmp"
: etc
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The poor indentation and layout of your script muddles the question, but the basic answer is
printf '%s|%s|%s|%sn' "$(field1)" "$(field2)" "$(field3") "$(field4)"
Refactored into this, and with indentation etc cleaned up, your script becomes
#!/bin/bash
host()
hostname
protocol()
# avoid useless use of cat; get rid of unused parameter
# ... do you need sudo here too?
grep Protocol /etc/ssh/sshd_config
rootlogin()
# straighten out massive spaghetti pretzel; remove unused parameter
# ... can you avoid sudo cat here?
if sudo cat /etc/ssh/sshd_config
printf '%s|%s|%s|%sn' "$1" "$(host)" "$(protocol)" "$(rootlogin)" >fonk.out
The last line is somewhat speculative; your current script doesn't seem to print the first field at all, and it's not clear what it's supposed to contain.
This no longer uses a temporary file, but one antipattern in your attempt was to create the temp file in an unrelated function. When you really do need a temp file, it would probably be a good idea to create it separately, then use it as a parameter everywhere. Like
tmp=$(mktemp) || exit
# arrange for temp file to be removed in case of errors or signals, too
trap 'rm -f "$tmp"' EXIT
trap 'exit' ERROR HUP QUIT TRAP TERM
function1 "$tmp"
function2 "$tmp"
: etc
The poor indentation and layout of your script muddles the question, but the basic answer is
printf '%s|%s|%s|%sn' "$(field1)" "$(field2)" "$(field3") "$(field4)"
Refactored into this, and with indentation etc cleaned up, your script becomes
#!/bin/bash
host()
hostname
protocol()
# avoid useless use of cat; get rid of unused parameter
# ... do you need sudo here too?
grep Protocol /etc/ssh/sshd_config
rootlogin()
# straighten out massive spaghetti pretzel; remove unused parameter
# ... can you avoid sudo cat here?
if sudo cat /etc/ssh/sshd_config
printf '%s|%s|%s|%sn' "$1" "$(host)" "$(protocol)" "$(rootlogin)" >fonk.out
The last line is somewhat speculative; your current script doesn't seem to print the first field at all, and it's not clear what it's supposed to contain.
This no longer uses a temporary file, but one antipattern in your attempt was to create the temp file in an unrelated function. When you really do need a temp file, it would probably be a good idea to create it separately, then use it as a parameter everywhere. Like
tmp=$(mktemp) || exit
# arrange for temp file to be removed in case of errors or signals, too
trap 'rm -f "$tmp"' EXIT
trap 'exit' ERROR HUP QUIT TRAP TERM
function1 "$tmp"
function2 "$tmp"
: etc
answered Aug 15 at 6:58
tripleee
4,84911626
4,84911626
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
add a comment |Â
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
thank you for your help and the information you provide.
â 1010111100011
Aug 15 at 7:04
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%2f462677%2fusing-multiple-function-to-get-an-output-in-a-single-line%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
You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends in
Protocol}
needs to look either likeProtocol; }
or the}
should go on the next line. You also do some unnecessary grepping to test ifPermitRootLogin
is set (you could anchor the pattern to the start with^PermitRootLogin yes
). But the main thing looks like a syntax error typo.â Kusalananda
Aug 15 at 6:29