forked from liaohuqiu/ssh-auto-login
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.sh
85 lines (76 loc) · 2 KB
/
base.sh
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
#!/bin/bash
#
current_dir=`pwd`
auto_gen_dir="$current_dir/auto_gen"
function exe_cmd()
{
echo $1
eval $1
}
function init()
{
if [ ! -d $HOME"/.ssh" ]; then
mkdir $HOME"/.ssh"
fi
if [ ! -d $auto_gen_dir ]; then
mkdir $auto_gen_dir
fi
local ssh_config="$HOME/.ssh/config"
local ssh_config_sample="$current_dir/files/ssh_config"
if [ ! -f $ssh_config ]; then
exe_cmd "cp -rf $ssh_config_sample $ssh_config"
else
local config_content=`cat $ssh_config_sample`
exe_cmd 'change_line append $ssh_config "ControlPath" "$config_content"';
fi
chmod 600 $ssh_config
}
function set_alias()
{
local bashrc=$HOME/.bash_profile
local alias_key=$1
local file=$2
if ! grep -q "alias $alias_key=" $bashrc; then
cmd="alias $alias_key=$file"
echo $cmd >> $bashrc
fi
exe_cmd "alias '$alias_key=$file'"
}
# mode file tag_str content
# https://github.com/liaohuqiu/work-anywhere/blob/master/base.sh
function change_line()
{
local mode=$1
local file=$2
local tag_str=$3
local content=$4
local file_bak=$file".bak"
local file_temp=$file".temp"
cp -f $file $file_bak
if [ $mode == "append" ]; then
grep -q "$tag_str" $file || echo "$content" >> $file
else
cat $file |awk -v mode="$mode" -v tag_str="$tag_str" -v content="$content" '
{
if ( index($0, tag_str) > 0) {
if ( mode == "after"){
printf( "%s\n%s\n", $0, content);
} else if (mode == "before")
{
printf( "%s\n%s\n", content, $0);
} else if(mode == "replace")
{
print content;
}
} else if ( index ($0, content) > 0)
{
# target content in line
# do nothing
} else
{
print $0;
}
}' > $file_temp
mv $file_temp $file
fi
}