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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:46:36+00:00 2026-05-26T11:46:36+00:00

I am writing a bash shell script that needs to take a file from

  • 0

I am writing a bash shell script that needs to take a file from the commandline: in the from of

./shell text.txt

The text.txt will contain 50 words. One on each line. You have to output to a new file with the words sorted and returned one on each line. How do I sort the file and output it to a new file

#!/bin/bash

badchoice () { MSG="Invalid Selection ... Please Try Again" ; } 
sort_file() { sort $1 -o sorted_file.txt;}
#---------------------------------
#This stores the unsorted file
#---------------------------------
FILE=""
# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
   fi
fi
# read $FILE using the file descriptors


#----------------------------------
# File input for the first perl script
#----------------------------------

perl_one=""
# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$2" == "" ]; then
 perl_one="/dev/stdin"
else
perl_one="$2"
# make sure file exist and readable
if [ ! -f $perl_one ]; then
echo "$perl_one : does not exists"
exit 1
elif [ ! -r $perl_one ]; then
echo "$perl_one: can not read"
exit 2
   fi
fi
# read $perl_one using the file descriptors

#----------------------------------------
# The input for the second perl function
#----------------------------------------

perl_two=""
# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$3" == "" ]; then
perl_two="/dev/stdin"
else
perl_two="$3"
# make sure file exist and readable
if [ ! -f $perl_two ]; then
echo "$perl_two : does not exists"
exit 1
elif [ ! -r $perl_two ]; then
echo "$perl_two: can not read"
exit 2
   fi
fi
# read perl_two using file descriptor




#This is the start of the menu
#---------------------------
# MENU PROMPTS
#---------------------------

menu(){
#clear screen
clear
echo `date`
echo "This is the shell script menu"
echo
echo "A) shell script sort"
echo "B) perl script sort"
echo "C) perl search for a word"
echo
echo "X) To exit the program"
echo
echo $MSG
echo
echo Select by pressing the letter and then ENTER ;

}


 while  true
 do
 # 1. display the menu
 menu

 # 2. read a line of input from the keyboard
 read answer

 # 3. Clear any error message
  MSG=

  case $answer in
   a|A) sort_file;;
 #       b|B) bpick;;
 #       c|C) cpick;;

 #      Ends the loop
   x|X) break;;

 #    If the entry was invalid call the badchoice function
 #    to initialize MSG to an error message
     *) badchoice;;

   esac
 #     Do it again until the user x
 done
  • 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-26T11:46:36+00:00Added an answer on May 26, 2026 at 11:46 am

    Except for proper quoting, you’ve already answered your own question:

    sort "$1" -o sorted_file.txt
    

    What’s the problem with that solution?

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

Sidebar

Related Questions

I am writing some BASH shell script that will continuously check a file to
I'm writing a bash script that needs to delete old files. It's currently implemented
I am writing a Bash shell script for Mac that sends an email notification
I'm writing a bash shell script that uses a case with three options: If
I am writing a bash script that needs to print the date of the
I am writing a bash script to copy a file from a remote server,
I'm writing a shell script (will become a cronjob) that will: 1: dump my
I am writing a bash script that calls functions declared in the parent shell,
I am writing a shell (bash) script and I'm trying to figure out an
I am writing a simple unix shell script: #!/bin/bash # abort the script if

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.