Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:13:37+00:00 2026-06-06T03:13:37+00:00

I try to generate a csv file through a lot of functions like that

  • 0

I try to generate a csv file through a lot of functions like that :

function get_sudo_version {
  sudo -V 2>/dev/null|grep -i "sudo version"
}
sudo_version=$(get_sudo_version)

Function above is a simple example but in some cases i cannot be sure the output is correct.
I would like to know what is the best way to validate the function return one text line only.

I thought about something like that

function validate_output {  
  output=$1; 
  echo $1|grep -q "\n"; 
  echo $?; 
}

mytest="val1
err2
err3"

But it’s obviously not working because the variable does not keep the retrun line character:

echo $mytest
val1 err2 err3

So if someone has a good idea of how i could wirte a generic check function i would be glad.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:13:40+00:00Added an answer on June 6, 2026 at 3:13 am

    If you have GNU grep, you could simply ensure that grep doesn’t produce more than one line of output in the first place via grep -m 1. Alternatively, use sed '/sudo version/!d;q' instead of grep.

    A function that simply checks lines of input while passing them through might look like:

    shopt -s lastpipe # if bash
    
    # ksh or bash
    validate_output() { 
        (( $(tee >(wc -l) >&2) == 1 ))
    } 2>&1
    
    # bash
    validate_output2() {
        local lines
        tee /dev/fd/2 | mapfile -tc1 -C '((++lines < 2)) || return; :'
        ((lines))
    } 2>&1
    
    get_sudo_version | validate_output || echo failed
    

    Many variations on that possible of course. IMO it’s pretty pointless and shouldn’t be used for something like this. Just design your get_sudo_version so that it guarantees the right results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create a CSV file by getting a datatable then looping through that datatable
I want to load a CSV file that looks like this: Acct. No.,1-15 Days,16-30
I try to debug a javascript function called from Generate Thumbnails plugin in Wordpress.
I have a script that generates a csv file using the following code: header('Content-type:
I try to generate Turkish spell file for Vim from a word list with
So, my code generates a CSV file using PHP's built-in fputcsv function. For the
I'm writing a small application that generates a CSV file for import into MYOB.
I have a button in a form tag that generates a CSV file and
I have a Web site (PHP) that generates a CSV file (text/csv) with the
I try generate dbml from sdf file. So I localize sqlMetal util in: C:\Program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.