請教bash script問題 - Linux

By Xanthe
at 2009-07-24T22:25
at 2009-07-24T22:25
Table of Contents
※ 引述《prolog (波卡)》之銘言:
: 假設我有一個list 其內容為:
: a1 a2 a3
: b1 b2 b3
: c1 c2 c3
: 我希望用cat list 將內容存成變數
: 希望${file1}為a1 b1 c1那一列 ${file2}為a2 b2 c2那一列...
: 請問我該如何做?
: 如果只有一列我目前知道,但將不同列存成不同變數目前沒有頭緒
: #!/bin/bash
: for FILE in `cat list`
: do
: xxx
: done
: 以上 謝謝!
lloyd@hilltop$ cat list
a1 a2 a3
b1 b2 b3
c1 c2 c3
lloyd@hilltop$ sh list1.sh
This file1 = a1 b1 c1
This file2 = a2 b2 c2
lloyd@hilltop$ cat list1.sh
#!/bin/sh
file1=""
file2=""
IFS=""
for i in $(cat list) ; do
L1=$(echo $i|cut -d" " -f1)
L2=$(echo $i|cut -d" " -f2)
file1="$file1 $L1"
file2="$file2 $L2"
done
unset IFS
echo "This file1 = $file1"
echo "This file2 = $file2"
lloyd@hilltop$
--
: 假設我有一個list 其內容為:
: a1 a2 a3
: b1 b2 b3
: c1 c2 c3
: 我希望用cat list 將內容存成變數
: 希望${file1}為a1 b1 c1那一列 ${file2}為a2 b2 c2那一列...
: 請問我該如何做?
: 如果只有一列我目前知道,但將不同列存成不同變數目前沒有頭緒
: #!/bin/bash
: for FILE in `cat list`
: do
: xxx
: done
: 以上 謝謝!
lloyd@hilltop$ cat list
a1 a2 a3
b1 b2 b3
c1 c2 c3
lloyd@hilltop$ sh list1.sh
This file1 = a1 b1 c1
This file2 = a2 b2 c2
lloyd@hilltop$ cat list1.sh
#!/bin/sh
file1=""
file2=""
IFS=""
for i in $(cat list) ; do
L1=$(echo $i|cut -d" " -f1)
L2=$(echo $i|cut -d" " -f2)
file1="$file1 $L1"
file2="$file2 $L2"
done
unset IFS
echo "This file1 = $file1"
echo "This file2 = $file2"
lloyd@hilltop$
--
Tags:
Linux
All Comments

By Carolina Franco
at 2009-07-25T08:52
at 2009-07-25T08:52
Related Posts
Fedora 11 Display setup

By Ula
at 2009-07-24T15:45
at 2009-07-24T15:45
DRBL/Clonezilla「集中管理環境」進階課程(台中)

By Jessica
at 2009-07-24T15:29
at 2009-07-24T15:29
USB隨身碟讀取.存取

By Ida
at 2009-07-24T14:58
at 2009-07-24T14:58
pidgin不能使用yahoo messenger

By Daniel
at 2009-07-24T13:23
at 2009-07-24T13:23
無法yum

By Isla
at 2009-07-24T13:16
at 2009-07-24T13:16