在 bash 中创建文件并替换变量

在 bash 中创建文件并替换变量

背景:我为自己创建了 VPN 服务。我有很多空闲时间,并且喜欢创造“类似商业”服务的挑战,因此我可以为自己创建一个投资组合。我创建这个脚本是为了创建 OpenVPN 的客户端配置文件,并希望清理它。它很草率,但现在它可以工作了,我正在努力清理它。

我的问题:我想创建一个预先确定的文件供客户使用。必须根据先前的变量将其中的某些数据添加到其中。

当前脚本:

#!/bin/bash

### Set PATH ###
BACKUP=/var/BACKUPS
OVPN=/etc/openvpn/
CA=/etc/openvpn/ca.crt
RSA=/etc/openvpn/easy-rsa
KEYS=/etc/openvpn/easy-rsa/keys
HTML=/var/dir/for/user/dl

### Move to working directory ###
cd $RSA

### Trial/30 day/90 day/365 day ###
PS3='What kind of subscriber is being created?: '
options=("7 Day Trial" "30 Day Subscriber" "90 Day Subscriber" "1 Year Subscriber" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "7 Day Trial")
            echo -e "You have selected 7 day trial.\nUsing vars7."
            source ./vars7
            break
            ;;
        "30 Day Subscriber")
            echo -e "You have selected 30 day subscriber.\nUsing vars30."
            source ./vars30
            break
            ;;
        "90 Day Subscriber")
            echo -e "You have selected 90 day subscriber.\nUsing vars90."
            source ./vars90
            break
            ;;
        "1 Year Subscriber")
            echo -e "You have selected 1 year subscriber.\nUsing vars365."
            source ./vars365
            break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

### Catch input for user and build/sign keys and certificates ###
echo "Please enter the user's name:"
read USER
echo "Starting process now..." 
./build-key $USER

### Set new PATHs ###
BASECRT=$(sed -n 68,97p $KEYS/$USER.crt)
BASECA=$(cat $CA)
BASEKEY=$(cat $KEYS/$USER.key)

### create stream-lined option file ###
cat > $OVPN/$USER.ovpn <<EOF
client
;dev tap
dev tun
;proto tcp
proto udp
remote SERVER 1194
remote SERVER 1194
remote-random
resolv-retry infinite
nobind
;user nobody
;group nogroup
persist-key
persist-tun
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
;mute-replay-warnings
ca ca.crt
cert $USER.crt
key $USER.key
ns-cert-type server
;tls-auth ta.key 1
;cipher x
comp-lzo
verb 3
;mute 20
<ca>
$BASECA
</ca>
<cert>
$BASECRT
</cert>
<key>
$BASEKEY
</key>

EOF

### Make Directories needed ###
mkdir $HTML/$USER
mkdir $BACKUP/$USER
mkdir $HTML/$USER/keys

### Store Backups ###
cp $KEYS/$USER.* $BACKUP/$USER
cp $OVPN/$USER.ovpn $BACKUP/$USER
tar -czvf $BACKUP/$USER.tar.gz $BACKUP/$USER

### Move keys and options to user's directory for download ###
cp $KEYS/$USER.* $HTML/$USER/keys
cp $OVPN/$USER.ovpn $HTML/$USER
cp $CA $HTML/$USER/keys
chmod 755 $HTML/$USER/*

我曾经用来cat > $OVPN/$USER.ovpn << EOF创建流线型USER.ovpn. $BASECRT$BASEKEY等被替换为以 base64 编码的各自文件。

我想从我的脚本中删除这一巨大的文本块。我尝试过使用cat < shellfile > Newfile- 但这不会替换变量,而是将它们添加到新文件中$BASECRT,如 等

现在我可以使用 'sed' 读取输出到客户端文件中的第一组行,cat 附加到客户端文件,然后 sed 接下来的几行,重复此过程,直到获得完整的客户端文件。

但必须有一种更简单、更清洁的方法来做到这一点。我从来没有必要创建这样的长文件。谢谢。 :)

答案1

您可以使用source命令,将创建精简的选项文件命令放入单独的文件中,并且来源当所有使用的变量都被分配后,它进入主脚本,

# USER.ovpn 

### create stream-lined option file ###
cat > $OVPN/$USER.ovpn <<EOF
client
;dev tap
dev tun
;proto tcp
proto udp
remote SERVER 1194
remote SERVER 1194
remote-random
resolv-retry infinite
nobind
;user nobody
;group nogroup
persist-key
persist-tun
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
;mute-replay-warnings
ca ca.crt
cert $USER.crt
key $USER.key
ns-cert-type server
;tls-auth ta.key 1
;cipher x
comp-lzo
verb 3
;mute 20
<ca>
$BASECA
</ca>
<cert>
$BASECRT
</cert>
<key>
$BASEKEY
</key>

EOF

然后你的主脚本可以做,

#!/bin/bash

### Set PATH ###
BACKUP=/var/BACKUPS
OVPN=/etc/openvpn/
CA=/etc/openvpn/ca.crt
RSA=/etc/openvpn/easy-rsa
KEYS=/etc/openvpn/easy-rsa/keys
HTML=/var/dir/for/user/dl

### Move to working directory ###
cd $RSA

### Trial/30 day/90 day/365 day ###
PS3='What kind of subscriber is being created?: '
options=("7 Day Trial" "30 Day Subscriber" "90 Day Subscriber" "1 Year Subscriber" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "7 Day Trial")
            echo -e "You have selected 7 day trial.\nUsing vars7."
            source ./vars7
            break
            ;;
        "30 Day Subscriber")
            echo -e "You have selected 30 day subscriber.\nUsing vars30."
            source ./vars30
            break
            ;;
        "90 Day Subscriber")
            echo -e "You have selected 90 day subscriber.\nUsing vars90."
            source ./vars90
            break
            ;;
        "1 Year Subscriber")
            echo -e "You have selected 1 year subscriber.\nUsing vars365."
            source ./vars365
            break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

### Catch input for user and build/sign keys and certificates ###
echo "Please enter the user's name:"
read USER
echo "Starting process now..." 
./build-key $USER

### Set new PATHs ###
BASECRT=$(sed -n 68,97p $KEYS/$USER.crt)
BASECA=$(cat $CA)
BASEKEY=$(cat $KEYS/$USER.key)

### create stream-lined option file ###

source USER.ovpn     # ". USER.ovpn" also works

### Make Directories needed ###
mkdir $HTML/$USER
mkdir $BACKUP/$USER
mkdir $HTML/$USER/keys

### Store Backups ###
cp $KEYS/$USER.* $BACKUP/$USER
cp $OVPN/$USER.ovpn $BACKUP/$USER
tar -czvf $BACKUP/$USER.tar.gz $BACKUP/$USER

### Move keys and options to user's directory for download ###
cp $KEYS/$USER.* $HTML/$USER/keys
cp $OVPN/$USER.ovpn $HTML/$USER
cp $CA $HTML/$USER/keys
chmod 755 $HTML/$USER/*
I had used cat > $OVPN/$USER.ovpn 

相关内容