Why getting the characters of a file list returns me also the files and folders of the current directory?

StackOverflow https://stackoverflow.com/questions/22708738

Domanda

I have the following script called fileok.sh to get all the different chars of a file based in this response:

#!/bin/bash

function get_ascii() {
    LC_CTYPE=C printf '%d' "'$1"
}

myfile="$1"

min_ascii=255
max_ascii=0
i=0

arrchar=( $(fold -w1 "$myfile" | sort -u) )
arrasc=()

for c in "${arrchar[@]}"; do
    ascii=$(get_ascii "$c")
    echo "$i: $ascii  $c"

    if [[ "$ascii" -gt "$max_ascii" ]]; then
        max_ascii="$ascii"
    fi
    if [[ "$ascii" -lt "$min_ascii" ]]; then
        min_ascii="$ascii"
    fi

    let i++
    arrasc+=( "$ascii" )
done

echo
echo "CHARS: ${arrchar[*]}"
echo
echo "ASCII: ${arrasc[*]}"
echo
echo "RANGE: $min_ascii-$max_ascii"
echo

This is the result:

0: 8
1: 8 ▒
2: 8 -
3: 8 &
4: 8 ▒
5: 8 ▒
6: 8 a
7: 8 ▒
8: 8 ▒
9: 8 b
10: 8 ▒
11: 8 e
12: 8 ▒
13: 8 ▒
14: 8 j
15: 8 ▒
16: 8 ▒
17: 96  `
18: 60  <
19: 166  ▒
20: 176  ▒
21: 45  -
22: 47  /
23: 46  .
24: 64  @
25: 100  freefic.sh
26: 100  freedoa.sh
27: 100  ret.sh
28: 69   file1.txt
29: 102  fileok.sh
30: 104  hs_err_pid21874.log
31: 104  hs_err_pid40826.log
32: 106  ref_lista2.txt
33: 108  lista_jcl.txt
34: 112  prueba
35: 83   myfolder2
36: 116  tsoioa_prueba.sh
37: 116  tsoioa.sh
38: 119  workspace
39: 88  rxpress2
40: 88  rxpress22
41: 88  rxpress23
42: 88  rxpress24
43: 88  rxpress2434
44: 88  rxpress24543
45: 88  rxpress244534
46: 88  rxpress243453
47: 88  rxpress24345345
48: 88  rxpress24345345
49: 88  rxpress2434555
50: 38  &
51: 35  #
52: 37  %
53: 1
54: 2
55: 3
56: 4
57: 12

58: 16
59: 17
60: 18
61: 19
62: 20
63: 22
64: 23
65: 24
66: 25
67: 27
8: 28
69: 127
70: 128  ▒
71: 129  ▒
72: 129  %
73: 130  ▒
74: 131  ▒
75: 132  ▒
76: 133  ▒
77: 134  ▒
78: 135  ▒
79: 136  ▒
80: 137  ▒
81: 140  ▒
82: 141  ▒
83: 144  ▒
84: 145  ▒
85: 147  ▒
86: 148  ▒
87: 149  ▒
88: 150  ▒
89: 151  ▒
90: 152  ▒
91: 153  ▒
92: 156  ▒
93: 157  ▒
94: 48  0
95: 49  1
96: 50  2
97: 51  3
98: 52  4
99: 53  5
100: 54  6
101: 55  7
102: 56  8
103: 57  9
104: 97  a
105: 65  A
106: 225  ▒
107: 193  ▒
108: 224  ▒
109: 192  ▒
110: 226  ▒
111: 194  ▒
112: 229  ▒
113: 197  ▒
114: 228  ▒
115: 196  ▒
116: 227  ▒
117: 195  ▒
118: 230  ▒
119: 132  ▒
120: 98  b
121: 66  B
122: 99  c
123: 67  C
124: 231  ▒
125: 199  ▒
126: 100  d
127: 68  D
128: 240  ▒
129: 101  e
130: 69  E
131: 233  ▒
132: 201  ▒
133: 232  ▒
134: 200  ▒
135: 234  ▒
136: 202  ▒
137: 235  ▒
138: 203  ▒
139: 102  f
140: 70  F
141: 103  g
142: 71  G
143: 104  h
144: 72  H
145: 105  i
146: 73  I
147: 237  ▒
148: 205  ▒
149: 236  ▒
150: 238  ▒
151: 206  ▒
152: 239  ▒
153: 207  ▒
154: 106  j
155: 74  J
156: 107  k
157: 75  K
158: 108  l
159: 76  L
160: 109  m
161: 77  M
162: 110  n
163: 78  N
164: 111  o
165: 79  O
166: 248  ▒
167: 216  ▒
168: 112  p
169: 80  P
170: 113  q
171: 81  Q
172: 114  r
173: 82  R
174: 83  S
175: 223  ▒
176: 84  T
177: 85  U
178: 86  V
179: 87  W
180: 88  X
181: 89  Y
182: 90  Z
183: 160  ▒

CHARS:▒-&▒▒a▒▒b▒e▒▒j▒▒ ` < ▒ ▒ - / . @ dchfic.sh dchioa.sh dfsalida.sh ED14.LSEG.V1312 fileok.sh hs_err_pid21874.log hs_err_pid40826.log jcl_lista2.txt lista_jcl.txt prueba SISIB.XPREP.JCL tsoioa_prueba.sh tsoioa.sh workspace XR05.UNLCFT.X130823N XR05.UNLCFT.X130823N.D207 XR05.UNLCTF.X130823N XR05.UNLIRS.X130823N XR05.UNLIRS.X130823N.D118 XR05.UNLVUI.X130822N XR15.SYSDISC.LOAD3 XR15.SYSERR.LOAD2 XR15.SYSERR.LOAD3 XR15.SYSMAP.LOAD3 XR15.SYSUT1.LOAD3 & # %
                              ▒ ▒ % ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ 0 1 2 3 4 5 6 7 8 9 a A ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ b B c C ▒ ▒ d D ▒ e E ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ f F g G h H i I ▒ ▒ ▒ ▒ ▒ ▒ ▒ j J k K l L m M n N o O ▒ ▒ p P q Q r R S ▒ T U V W X Y Z ▒

ASCII: 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 96 60 166 176 45 47 46 64 100 100 100 69 102 104 104 106 108 112 83 116 116 119 88 88 88 88 88 88 88 88 88 88 88 38 35 37 1 2 3 4 12 16 17 18 19 20 22 23 24 25 27 28 127 128 129 129 130 131 132 133 134 135 136 137 140 141 144 145 147 148 149 150 151 152 153 156 157 48 49 50 51 52 53 54 55 56 57 97 65 225 193 224 192 226 194 229 197 228 196 227 195 230 132 98 66 99 67 231 199 100 68 240 101 69 233 201 232 200 234 202 235 203 102 70 103 71 104 72 105 73 237 205 236 238 206 239 207 106 74 107 75 108 76 109 77 110 78 111 79 248 216 112 80 113 81 114 82 83 223 84 85 86 87 88 89 90 160

RANGE: 1-248

Where:

25: 100  freefic.sh
26: 100  freedoa.sh
27: 100  ret.sh
28: 69   file1.txt
29: 102  fileok.sh
30: 104  hs_err_pid21874.log
31: 104  hs_err_pid40826.log
32: 106  ref_lista2.txt
33: 108  lista_jcl.txt
34: 112  prueba
35: 83   myfolder2
36: 116  tsoioa_prueba.sh
37: 116  tsoioa.sh
38: 119  workspace
39: 88  rxpress2
40: 88  rxpress22
41: 88  rxpress23
42: 88  rxpress24
43: 88  rxpress2434
44: 88  rxpress24543
45: 88  rxpress244534
46: 88  rxpress243453
47: 88  rxpress24345345
48: 88  rxpress24345345
49: 88  rxpress2434555

Are the files of the current directory where I execute the script. What I'm missing? Why doing this returns me an ls of the current directory?

May be an unicode character is doing it?

È stato utile?

Soluzione

The problem is this assignment

arrchar=( $(fold -w1 "$myfile" | sort -u) )

where you rely on word splitting to populate the array per character, unfortunately it also expands * so if your file in question contains an asterisk the array will list the files in your current directory instead of a literal *.

You can e.g. temporarily disable globbing to avoid this, for example

set -o noglob
arrchar=( $(fold -w1 "$myfile" | sort -u) )
set +o noglob

or use read to directly read into the array via process substitution:

read -a arrchar -d '' < <(fold -w1 t.txt | sort -u)

Altri suggerimenti

It is definitely related to the * character. One workaround is running the script inside an empty directory, ie.

mkdir empty_dir
cd empty_dir
sh /path/to/fileok.sh /path/to/input_file

If location of the * character is not important, this one does the job:

#!/bin/bash

function get_ascii() {
    LC_CTYPE=C printf '%d' "'$1"
}

myfile="$1"

min_ascii=255
max_ascii=0
i=0

test -n "`grep '*' "$myfile"`" && CONTAINSSTAR=true
arrchar=( $(fold -w1 "$myfile" | grep -v '*'|sort -u) )
arrasc=()



for c in "${arrchar[@]}"; do
    ascii=$(get_ascii "$c")
    echo "$i: $ascii  $c"

    if [[ "$ascii" -gt "$max_ascii" ]]; then
        max_ascii="$ascii"
    fi
    if [[ "$ascii" -lt "$min_ascii" ]]; then
        min_ascii="$ascii"
    fi

    let i++
    arrasc+=( "$ascii" )
done

if [ "$CONTAINSSTAR" == "true" ];then 
    echo -e "$i: 42  *"
    echo
    echo -e "CHARS: ${arrchar[*]} *"
    echo
    echo "ASCII: ${arrasc[*]} 42"
else
    echo
    echo "CHARS: ${arrchar[*]}"
    echo
    echo "ASCII: ${arrasc[*]}"

fi
echo
echo "RANGE: $min_ascii-$max_ascii"
echo
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top