使用 Bourne Shell 从存档中读取

发布于 2024-09-24 02:11:25 字数 1122 浏览 5 评论 0原文

我第一次尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2024-10-01 02:11:25

我已经修正了你的一些脚本。在将它们存储到数组之前,您需要从每行中剪切出姓名和薪水字段。

#!/bin/bash

process_file()
{
file=$1;
counter=0
while read line
do
        #the name is the first two fields
        name=`echo $line | cut -d' ' -f1,2`
        NAMEARRAY[$counter]="$name"

        #the salary is the third field
        salary=`echo $line | cut -d' ' -f3`
        SALARYARRAY[$counter]="$salary"

        echo ${NAMEARRAY[$counter]}:${SALARYARRAY[$counter]}

        counter=$(($counter+1))
done < $file
}


##
# Main Script Body
#
# Takes a filename input by user, passes to process_file()
##

PROGRAMTITLE="Account Processing Shell Script (APS)"
echo $PROGRAMTITLE

echo -n "Please specify filename for processing: "
read FILENAME

if [ -r $FILENAME ] #check that the file exists and is readable
then
        process_file $FILENAME
else
        echo "Error reading file $FILENAME"
fi

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.

#!/bin/bash

process_file()
{
file=$1;
counter=0
while read line
do
        #the name is the first two fields
        name=`echo $line | cut -d' ' -f1,2`
        NAMEARRAY[$counter]="$name"

        #the salary is the third field
        salary=`echo $line | cut -d' ' -f3`
        SALARYARRAY[$counter]="$salary"

        echo ${NAMEARRAY[$counter]}:${SALARYARRAY[$counter]}

        counter=$(($counter+1))
done < $file
}


##
# Main Script Body
#
# Takes a filename input by user, passes to process_file()
##

PROGRAMTITLE="Account Processing Shell Script (APS)"
echo $PROGRAMTITLE

echo -n "Please specify filename for processing: "
read FILENAME

if [ -r $FILENAME ] #check that the file exists and is readable
then
        process_file $FILENAME
else
        echo "Error reading file $FILENAME"
fi
泪意 2024-10-01 02:11:25

给定您指定的文件格式,每条记录具有三个字段:first、last 和 amount,然后:

i=0
while read fistname lastname amount; do
    NAMEARRAY[$i]="$firstname $lastname"
    SALARYARRAY[$i]=$amount
    i = `expr $i + 1`
done < "$FILE"

shell 内置读取,自动分割输入。请参阅 sh(1) 手册页中的变量 IFS。如果 amount 字段后面有数据,并且您希望忽略它,只需在 amount 后面创建另一个变量即可;但不要使用它。它将把前 3 个字段之后的所有内容收集到额外变量中。

您指定了 Bourne shell,所以我使用了一些非常过时的东西:

i=`expr $x + 1`

通常是

let $((i++))   # ksh, bash (POSIX, Solaris, Linux)

在现代系统上编写的,/bin/sh 通常是 ksh 或非常兼容的东西。您可能可以使用 let $((i++))

Given the file format you specified, each record has three fields first last and amount, then:

i=0
while read fistname lastname amount; do
    NAMEARRAY[$i]="$firstname $lastname"
    SALARYARRAY[$i]=$amount
    i = `expr $i + 1`
done < "$FILE"

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:

i=`expr $x + 1`

is usually written

let $((i++))   # ksh, bash (POSIX, Solaris, Linux)

On modern systems, /bin/sh is usually ksh or something pretty compatible. You can probably use let $((i++))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文