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 7444507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:31:48+00:00 2026-05-29T11:31:48+00:00

I have a script that prints in a loop. I want the loop to

  • 0

I have a script that prints in a loop. I want the loop to print differently the first time from all other times (i.e., it should print differently if anything has been printed at all). I am thinking a simple way would be to check whether anything has been printed yet (i.e., stdout has been written to). Is there any way to determine that?

I know I could also write to a variable and test whether it’s empty, but I’d like to avoid a variable if I can.

  • 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-05-29T11:31:49+00:00Added an answer on May 29, 2026 at 11:31 am

    I think that will do what you need. If you echo something between # THE SCRIPT ITSELF and # END, THE FOLLOWING DATA HAS BEEN WRITTEN TO STDOUT will be printed STDOUT HAS NOT BEEN TOUCHED else…

    #!/bin/bash
    
    readonly TMP=$(mktemp /tmp/test_XXXXXX)
    
    exec 3<> "$TMP"   # open tmp file as fd 3
    exec 4>&1         # save current value of stdout as fd 4
    exec >&3          # redirect stdout to fd 3 (tmp file)
    
    # THE SCRIPT ITSELF
    
    echo Hello World
    
    # END
    
    exec >&4          # restore save stdout
    exec 3>&-         # close tmp file
    
    TMP_SIZE=$(stat -f %z "$TMP")
    if [ $TMP_SIZE -gt 0 ]; then
        echo "THE FOLLOWING DATA HAS BEEN WRITTEN TO STDOUT"
        echo
        cat "$TMP"
    else
        echo "STDOUT HAS NOT BEEN TOUCHED"
    fi
    
    rm "$TMP"
    

    So, output of the script as is:

    
    THE FOLLOWING DATA HAS BEEN WRITTEN TO STDOUT
    
    Hello World
    

    and if you remove the echo Hello World line:

    STDOUT HAS NOT BEEN TOUCHED
    

    And if you really want to test that while running the script itself, you can do that, too 🙂

    #!/bin/bash
    
    #FIRST ELSE
    function echo_fl() {
        TMP_SIZE=$(stat -f %z "$TMP")
        if [ $TMP_SIZE -gt 0 ]; then
            echo $2
        else
            echo $1
        fi  
    }
    
    TMP=$(mktemp /tmp/test_XXXXXX)
    
    exec 3 "$TMP"   # open tmp file as fd 3
    exec 4>&1         # save current value of stdout as fd 4
    exec >&3          # redirect stdout to fd 3 (tmp file)
    
    # THE SCRIPT ITSELF
    
    for f in fst snd trd; do
        echo_fl "$(echo $f | tr a-z A-Z)" "$f"
    done
    
    # END
    
    exec >&4          # restore save stdout
    exec 3>&-         # close tmp file
    
    TMP_SIZE=$(stat -f %z "$TMP")
    if [ $TMP_SIZE -gt 0 ]; then
        echo "THE FOLLOWING DATA HAS BEEN WRITTEN TO STDOUT"
        echo
        cat "$TMP"
    else
        echo "STDOUT HAS NOT BEEN TOUCHED"
    fi
    
    rm "$TMP"
    

    output is:

    THE FOLLOWING DATA HAS BEEN WRITTEN TO STDOUT
    
    FST
    snd
    trd
    

    as you can see: Only the first line (FST) has caps on. That’s what the echo_fl function does for you: If it’s the first line of output, if echoes the first argument, if it’s not it echoes the second argument 🙂

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

Sidebar

Related Questions

I have a CGI script that prints the following on stdout: print Status: 302
I have a script (worker.py) that prints unbuffered output in the form... 1 2
I have a script that retrieves objects from a remote server through an Ajax
I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that renders graphs in gnuplot. The graphs all end up
I have a pagination script that displays a list of all pages like so:
I want to create a single shape file from multiple mxd's that have multiple
I have a php script that takes hierarchical data from a mysql procedure and
I have written a simple script that calls a function in a while loop.
I have a script to print appointments from a mysql db, There is 2

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.