English 中文(简体)
阅读使用手稿的文档
原标题:reading a file using shell script
  • 时间:2012-01-16 06:21:10
  •  标签:
  • linux
  • shell

我有一份名为<代码>sqlfile的案文,内容如下:

a.sql
b.sql
c.sql
d.sql

What I want is that to store them in variables and then print using for loop. But here I get only d.sql in the output of the script.

文字:

#!/bin/bash

while read line
do
files=`echo $line`
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile

for file in $files
        do
                echo $file
        done
问题回答

A variable can only hold one element, what you want is an array

#!/bin/bash

while read line
do
  files+=( "$line" )
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile

for file in "${files[@]}"
do
  echo "$file"
done
while read line
do files="$files $line"
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile

files=$(</home/abdul_old/Desktop/My_Shell_Script/sqlfile)

files=$(cat /home/abdul_old/Desktop/My_Shell_Script/sqlfile)

You re doing way too much w或k in your loop.

The middle alternative w或ks with bash; the other two w或k with most shells. Prefer $(...) to back-quotes.

This code assumes there are no spaces in file names to mess things up. If you do use blanks in file names, you have to w或k marginally harder - see the array-based solution by SiegeX

I think you need to make the "files" as array. otherwise, as soon as the while finishes, "files" stores the latest "line". try:

files=( "${files[@]}" $line )

这一权利,你将最后价值视为“档案”。

你必须使用+=而不是 =

#!/bin/bash

while read line
do
files+=`echo " $line"`
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile

for file in $files
        do
                echo $file
        done

使用读物是罚款,但你必须首先将国际住房基金会的环境变数从每一行中取出:。 保留主要的白色空间,同时进行阅读和研读; 编造按照Bash/a的行文。

你们都必须:

readarray myData < sqlfile

This will put file lines into an array called myData
Now you can access any of these lines like this:

printf "%s
" "${myData[0]}" #outputs first line
printf "%s
" "${myData[2]}" #outputs third line

并且,你可以放弃:

for curLine in "${myData[@]}"; do
    echo "$curLine"
done

Note that these lines would contain character as well. To remove trailing newlines you can use -t flag like this:

readarray -t myData < sqlfile

readarray is a synonym to mapfile. You can read about it in man bash





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签