Сценарий оболочки Bash, чтобы взять текстовый файл и вывести отсортированный файл - PullRequest
1 голос
/ 25 октября 2011

Я пишу сценарий оболочки bash, который должен получить файл из командной строки: из of

./shell text.txt

text.txt будет содержать 50 слов. По одному на каждой строке. Вы должны вывести в новый файл слова, отсортированные и возвращенные по одному в каждой строке. Как отсортировать файл и вывести его в новый файл

#!/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

Ответы [ 2 ]

2 голосов
/ 25 октября 2011

Это будет что-то вроде следующего:

#!/usr/bin/env bash

# Check to ensure exactly one argument passed.

if [[ $# -ne 1 ]] ; then
        echo "Usage: shell <inputFile>"
        exit 1
fi

# Check that the argument is an existing regular file.

if [[ ! -f $1 ]] ; then
        echo "Error: '$1' not a regular file"
        exit 1
fi

# Sort it, sending output to xyzzy.new, where xyzzy was the original file.

sort $1 >$1.new
1 голос
/ 25 октября 2011

За исключением правильного цитирования, вы уже ответили на свой вопрос:

sort "$1" -o sorted_file.txt

В чем проблема с этим решением?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...