简体   繁体   English

在 Bash 脚本中使用 Vim 命令

[英]Using Vim commands in a Bash Script

so im trying to create a bash script that runs on MAC command line to a remote server and uses some mv commands to move some files around but i also need it to open up a file and add a line to the top of the file and save it in the middle of the script heres what i have so far:所以我试图创建一个 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 '1i\n%s\n.\nwq\n' "$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

Don't know if you're using echo to output the prompts because you didn't know about the -p read option or you wanted the new lines.不知道您是否正在使用 echo to output 提示,因为您不知道-p read option或者您想要新行。

You could use command grouping to add a line at the top of your file.您可以使用命令分组在文件顶部添加一行。

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 could be used too, if the line had to go to a specific line: sed也可以使用,如果线路必须 go 到特定线路:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM