Bash shell 脚本接收文本文件并输出排序后的文件
我正在编写一个bash shell脚本,需要从命令行获取一个文件:在
./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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大致如下:
That would be something along the following lines:
除了正确引用之外,您已经回答了自己的问题:
该解决方案有什么问题?
Except for proper quoting, you've already answered your own question:
What's the problem with that solution?