Using Multiple Function to get an output in a single Line

The name of the pictureThe name of the pictureThe name of the pictureClash 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"









share|improve this question























  • 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














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"









share|improve this question























  • 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












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"









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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















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










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





share|improve this answer




















  • thank you for your help and the information you provide.
    – 1010111100011
    Aug 15 at 7:04










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%2f462677%2fusing-multiple-function-to-get-an-output-in-a-single-line%23new-answer', 'question_page');

);

Post as a guest






























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





share|improve this answer




















  • thank you for your help and the information you provide.
    – 1010111100011
    Aug 15 at 7:04














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





share|improve this answer




















  • thank you for your help and the information you provide.
    – 1010111100011
    Aug 15 at 7:04












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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

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

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay