回答編集履歴

10

変更

2017/07/24 06:45

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -82,7 +82,7 @@
82
82
 
83
83
 
84
84
 
85
- files='*.txt'
85
+ files='ls -1 *.txt'
86
86
 
87
87
  filearray=()
88
88
 
@@ -90,7 +90,7 @@
90
90
 
91
91
  IFS=$'\n'
92
92
 
93
- for filepath in ${files}; do
93
+ for filepath in `eval ${files}`; do
94
94
 
95
95
  if [ -f "${filepath}" ]; then
96
96
 

9

変更

2017/07/24 06:45

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -82,7 +82,7 @@
82
82
 
83
83
 
84
84
 
85
- files='ls -1 *.txt'
85
+ files='*.txt'
86
86
 
87
87
  filearray=()
88
88
 
@@ -90,7 +90,7 @@
90
90
 
91
91
  IFS=$'\n'
92
92
 
93
- for filepath in `eval ${files}`; do
93
+ for filepath in ${files}; do
94
94
 
95
95
  if [ -f "${filepath}" ]; then
96
96
 

8

追加

2017/07/24 06:41

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -71,3 +71,43 @@
71
71
  done
72
72
 
73
73
  ```
74
+
75
+
76
+
77
+ ブランク対応の別版
78
+
79
+ ```bash
80
+
81
+ #!/bin/bash
82
+
83
+
84
+
85
+ files='ls -1 *.txt'
86
+
87
+ filearray=()
88
+
89
+
90
+
91
+ IFS=$'\n'
92
+
93
+ for filepath in `eval ${files}`; do
94
+
95
+ if [ -f "${filepath}" ]; then
96
+
97
+ filearray+=("$filepath")
98
+
99
+ fi
100
+
101
+ done
102
+
103
+
104
+
105
+ echo "ファイル一覧"
106
+
107
+ for i in ${filearray[@]}; do
108
+
109
+ echo "$i"
110
+
111
+ done
112
+
113
+ ```

7

変更

2017/07/24 06:27

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -1,4 +1,4 @@
1
- ご提示されたスクリプトを少し修正してみました
1
+ ご提示されたスクリプトを少し修正してみました(間違っていたので修正しました)
2
2
 
3
3
  ```bash
4
4
 
@@ -6,19 +6,19 @@
6
6
 
7
7
 
8
8
 
9
- files='ls *.txt'
9
+ files='*.txt'
10
10
 
11
11
  filearray=()
12
12
 
13
13
 
14
14
 
15
- for filepath in `${files}`; do
15
+ for filepath in ${files}; do
16
16
 
17
- if [ -f "${filepath}" ]; then
17
+ if [ -f "${filepath}" ]; then
18
18
 
19
- filearray+=("$filepath")
19
+ filearray+=("$filepath")
20
20
 
21
- fi
21
+ fi
22
22
 
23
23
  done
24
24
 
@@ -28,9 +28,11 @@
28
28
 
29
29
  for i in ${filearray[@]}; do
30
30
 
31
- echo "$i"
31
+ echo "$i"
32
32
 
33
33
  done
34
+
35
+
34
36
 
35
37
  ```
36
38
 

6

変更

2017/07/24 06:23

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -40,11 +40,7 @@
40
40
 
41
41
  ```bash
42
42
 
43
- #!/bin/bash
44
-
45
-
46
-
47
- files='ls -1 *.txt'
43
+ files=`ls -1 *.txt`
48
44
 
49
45
  filearray=()
50
46
 
@@ -52,7 +48,7 @@
52
48
 
53
49
  IFS=$'\n'
54
50
 
55
- for filepath in `${files}`; do
51
+ for filepath in ${files}; do
56
52
 
57
53
  if [ -f "${filepath}" ]; then
58
54
 

5

変更

2017/07/24 06:16

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- files='ls *.txt'
47
+ files='ls -1 *.txt'
48
48
 
49
49
  filearray=()
50
50
 

4

追加

2017/07/24 06:12

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -36,19 +36,23 @@
36
36
 
37
37
 
38
38
 
39
+ ファイル名にブランクが入っているものが有るといけないのでIFSを付けました”hoge hoge.txt”
40
+
39
41
  ```bash
40
42
 
41
43
  #!/bin/bash
42
44
 
43
45
 
44
46
 
45
- files=`ls *.txt`
47
+ files='ls *.txt'
46
48
 
47
49
  filearray=()
48
50
 
49
51
 
50
52
 
53
+ IFS=$'\n'
54
+
51
- for filepath in ${files}; do
55
+ for filepath in `${files}`; do
52
56
 
53
57
  if [ -f "${filepath}" ]; then
54
58
 

3

変更

2017/07/24 06:11

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -50,9 +50,9 @@
50
50
 
51
51
  for filepath in ${files}; do
52
52
 
53
- if [ -f ${filepath} ]; then
53
+ if [ -f "${filepath}" ]; then
54
54
 
55
- filearray+=($filepath)
55
+ filearray+=("$filepath")
56
56
 
57
57
  fi
58
58
 

2

追加

2017/07/24 06:00

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -33,3 +33,39 @@
33
33
  done
34
34
 
35
35
  ```
36
+
37
+
38
+
39
+ ```bash
40
+
41
+ #!/bin/bash
42
+
43
+
44
+
45
+ files=`ls *.txt`
46
+
47
+ filearray=()
48
+
49
+
50
+
51
+ for filepath in ${files}; do
52
+
53
+ if [ -f ${filepath} ]; then
54
+
55
+ filearray+=($filepath)
56
+
57
+ fi
58
+
59
+ done
60
+
61
+
62
+
63
+ echo "ファイル一覧"
64
+
65
+ for i in ${filearray[@]}; do
66
+
67
+ echo "$i"
68
+
69
+ done
70
+
71
+ ```

1

修正

2017/07/24 05:59

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -1,6 +1,10 @@
1
1
  ご提示されたスクリプトを少し修正してみました
2
2
 
3
3
  ```bash
4
+
5
+ #!/bin/bash
6
+
7
+
4
8
 
5
9
  files='ls *.txt'
6
10