使用 Bourne Shell 从存档中读取
我第一次尝试使用 Bourne shell 脚本,但我似乎无法弄清楚如何将文本从文件保存到内部脚本变量。文件格式如下:
acc.text
Jason Bourne 213.4
Alice Deweger 1
Mark Harrington 312
我当前的脚本(可能完全不正确,因为我只是在 NotePad++ 中创建它而不使用实际的 shell 控制台)如下:
#!/bin/sh
process_file()
{
FILE = $1;
SALARYARRAY;
NAMEARRAY;
COUNTER = 0;
while read line
do
$NAMEARRAY[$COUNTER] =
$SALARYARRAY[$COUNTER] =
$COUNTER + 1;
echo $NAMEARRAY[$COUNTER]:$SALARYARRAY[$COUNTER];
done < "$FILE"
order_Map
}
# Function is not complete as of now, will later order SALARYARRAY in descending order
order_Map()
{
i = 0;
for i in $COUNTER
do
if ($SALARYARRAY[
done
}
##
# Main Script Body
#
# Takes a filename input by user, passes to process_file()
##
PROGRAMTITLE = "Account Processing Shell Script (APS)"
FILENAME = "acc.$$"
echo $PROGRAMTITLE
echo Please specify filename for processing
read $FILENAME
while(! -f $FILE || ! -r $FILE)
do
echo Error while attempting to write to file. Please specify file for processing:
read $FILENAME
done
echo Processing the file...
process_file $FILENAME
I am trying to use Bourne shell scripting for the first time ever, and I cannot seem to figure out determining how to save text from a file to internal script variables. The file format is as follows:
acc.text
Jason Bourne 213.4
Alice Deweger 1
Mark Harrington 312
The current script that I have (which might be ENTIRELY incorrect as I am simply creating it in NotePad++ without using an actual shell console) is as follows:
#!/bin/sh
process_file()
{
FILE = $1;
SALARYARRAY;
NAMEARRAY;
COUNTER = 0;
while read line
do
$NAMEARRAY[$COUNTER] =
$SALARYARRAY[$COUNTER] =
$COUNTER + 1;
echo $NAMEARRAY[$COUNTER]:$SALARYARRAY[$COUNTER];
done < "$FILE"
order_Map
}
# Function is not complete as of now, will later order SALARYARRAY in descending order
order_Map()
{
i = 0;
for i in $COUNTER
do
if ($SALARYARRAY[
done
}
##
# Main Script Body
#
# Takes a filename input by user, passes to process_file()
##
PROGRAMTITLE = "Account Processing Shell Script (APS)"
FILENAME = "acc.$"
echo $PROGRAMTITLE
echo Please specify filename for processing
read $FILENAME
while(! -f $FILE || ! -r $FILE)
do
echo Error while attempting to write to file. Please specify file for processing:
read $FILENAME
done
echo Processing the file...
process_file $FILENAME
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经修正了你的一些脚本。在将它们存储到数组之前,您需要从每行中
剪切
出姓名和薪水字段。I have fixed some of your script. You need to
cut
out the name and salary fields from each line before storing them into the array.给定您指定的文件格式,每条记录具有三个字段:first、last 和 amount,然后:
shell 内置读取,自动分割输入。请参阅 sh(1) 手册页中的变量 IFS。如果 amount 字段后面有数据,并且您希望忽略它,只需在 amount 后面创建另一个变量即可;但不要使用它。它将把前 3 个字段之后的所有内容收集到额外变量中。
您指定了 Bourne shell,所以我使用了一些非常过时的东西:
通常是
在现代系统上编写的,/bin/sh 通常是 ksh 或非常兼容的东西。您可能可以使用
let $((i++))
Given the file format you specified, each record has three fields first last and amount, then:
The shell read built-in, automatically splits input. See the variable IFS in the man page for sh(1). If you have data after the amount field, and you wish to ignore it, just create another variable after amount; but don't use it. It will collect everything after the 1st 3 fields into the extra variable.
You specified Bourne shell, so I used some pretty antiquated stuff:
is usually written
On modern systems, /bin/sh is usually ksh or something pretty compatible. You can probably use
let $((i++))