在Bash脚本中使用Vim命令



所以我试图创建一个bash脚本,在MAC命令行上运行到远程服务器,并使用一些mv命令来移动一些文件,但我也需要它来打开一个文件,并在文件的顶部添加一行,并将其保存在脚本的中间,这里是我迄今为止所拥有的:


(this is adjusting permissions so i can edit the file)
chef-client -r xxxxxxredactedxxxxxxredacted
cd /xxx/postgresql/xx/main/
Sudo chmod -R 775 filenamehere
sudo chown -R postgres:postgres filenamehere

read -p 'Enter the IP: ' ip

echo "Enter the file name :"
read -r filename
echo "Type this below: host all all "$ip"/24 trust : "
read -r line
cd /etc/postgresql/12/main/
printf '1in%sn.nwqn' "$line" | ed "$filename".   <-- **this is the problem line**

^ this command gives me permission denied because of access, for some reason i can edit it with vim but not this command
its worth noting these commands arent ran through my pc so my ability to move files is somewhat limited, its ran through SSM ing into an IP of a test enviroment through my command line
Normally I manually VIM into the file and add a line to the top

不知道您使用echo来输出提示是因为您不知道-p read option还是您想要新行。

您可以使用命令分组在文件的顶部添加一行。

read -p "Have you copied a file to the data shipper? (yes/no)"
if [ "$REPLY" == "yes" ]; then
read -p "Enter a variable: " VARIABLE

read -p "Enter a file name: " FILE

cd /var/xxxredacted////
cd /tmp/
sudo mv "$FILE" /varxxxredactedxxxxxxxx/drop
cd /var/redactedxxxxredactedxx/drop
sudo unzip "$FILE"
fi
read -p "Enter the file name:n" FILENAME
read -p "Enter the line to be added:n" LINE
{ echo $LINE; cat "$FILENAME"; } > "${FILENAME}.new"
mv "$FILENAME"{.new,}

sed也可以使用,如果该行必须转到特定行:

# If n is omnited, $LINE will just be inserted on 
# line 1 instead of appending a new line
sed -i "${LINENB}s/$LINEn/" $FILENAME

最新更新