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

  • Home
  • SEARCH
  • 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 9220831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:26:24+00:00 2026-06-18T03:26:24+00:00

I a new to bash but I am trying to write a bash script

  • 0

I a new to bash but I am trying to write a bash script which does the following:

write_to_file()
{
 #check if file exists
 # if not create the file
 # else open the file to edit
 # go in a while loop
 # ask input from user 
 # write to the end of the file
 # until user types  ":q"

 }

If anyone can point out the literature, I would be very thankful
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-18T03:26:25+00:00Added an answer on June 18, 2026 at 3:26 am

    Update: As it’s a bash question, you should try this first. 😉

    cat <<':q' >> test.file
    

    To understand what is going on, read about bash’s IO redirection, heredoc syntax and the cat command


    As you see above, there are many ways to do it. To explain some more bash commands I’ve prepared the function also in the way you’ve requested it:

    #!/bin/bash
    
    write_to_file()
    {
    
         # initialize a local var
         local file="test.file"
    
         # check if file exists. this is not required as echo >> would 
         # would create it any way. but for this example I've added it for you
         # -f checks if a file exists. The ! operator negates the result
         if [ ! -f "$file" ] ; then
             # if not create the file
             touch "$file"
         fi
    
         # "open the file to edit" ... not required. echo will do
    
         # go in a while loop
         while true ; do
            # ask input from user. read will store the 
            # line buffered user input in the var $user_input
            # line buffered means that read returns if the user
            # presses return
            read user_input
    
            # until user types  ":q" ... using the == operator
            if [ "$user_input" = ":q" ] ; then
                return # return from function
            fi
    
            # write to the end of the file. if the file 
            # not already exists it will be created
            echo "$user_input" >> "$file"
         done
     }
    
    # execute it
    write_to_file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to Bash scripting. I am trying to write a script
I am trying to write a bash script that will take a text file
I'm trying to write a bash script that imports a CSV file and sends
I'm creating a bash script to create new EC2 instances and then upload a
I am trying to write a simple bash script that will search for files
I have a bash file, which I’m trying to run in Cygwin on a
so what I'm trying to do is to write a Bash script that will
I am new to bash scripts. I'm just trying to make a script that
I'm new to bash scripts (and the *nix shell altogether) but I'm trying to
I'm trying to create a new table in postgres but when I do it

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.