linux bash tutorial

linux bash tutorial

Guide

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash

content=$(sed -n '4p' 1.csv)
echo $content

row=`wc -l < 2.csv`
echo $row

i=0
for id in `cat 2.csv`
do
if [ $id == $content ]
then
echo $id
else
echo 'not equal'
((i=i+1))
fi
done
echo $i

# array
echo '---------------------------------------'
arr=(Hello World)
echo ${arr[0]}
echo ${arr[1]}

arr[0]="xxx"
arr[1]="yyy"
echo ${arr[0]}
echo ${arr[1]}

echo '---------------------------------------'

# array2
array=(one two three four [5]=five)

echo "Array size: ${#array[*]}"

echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done

echo "Array indexes:"
for index in ${!array[*]}
do
printf " %d\n" $index
done

echo "Array items and indexes:"
for index in ${!array[*]}
do
printf "%4d: %s\n" $index ${array[$index]}
done

echo '---------------------------------------'

# array3
array=("first item" "second item" "third" "item")

echo "Number of items in original array: ${#array[*]}"
for ix in ${!array[*]}
do
printf " %s\n" "${array[$ix]}"
done
echo

# "${arr[*]}" returns all the items as a single word, whereas "${arr[@]}" returns each item as a separate word.

arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo

arr=("${array[*]}")
echo "After * quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo

arr=("${array[@]}")
echo "After @ quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done

Reference

History

  • 20190308: created.
坚持技术分享,您的支持将鼓励我继续创作!