bash - 如何在 Ubuntu 上的 bash 脚本中分隔命令行参数?

标签 bash ubuntu ssh remote-access

在我的 bash 脚本中,我想更改名为“test.txt”的特定文件的文件权限,该文件位于:

"/var/www/tomcat7/dir1/test.txt"

我的问题是,如果我要给出文件的完整路径,我想更改所有目录的权限,例如“var”、“www”、tomcat7”、“dir1”,最后是“test.txt” .

文件路径通过单独的文本文件作为命令行参数给出,这是我的代码,
setFilePErmission(){
    ssh ppuser@10.101.5.91 "sudo chmod 777 $1"
}

setFilePErmission $1

谁能帮我?谢谢你.... :)

最佳答案

#!/bin/bash 
setFilePErmission(){  
i=$(echo "$1" | awk -F '/' '{print NF}') 
y=$1 
while [[ $i -gt 1 ]] 
do 
ssh ppuser@10.101.5.91 "sudo chmod 777 $y" 
y=${y%/*} 
(( i-- )) 
done
}

setFilePErmission "your path goes here"

检查这是否适合您。

我仍然怀疑,为什么需要这样的权限..

请确保在运行这样的东西时,因为一旦您更改权限,除非您不记得每个文件权限,否则很难将它们恢复到以前的值。

关于bash - 如何在 Ubuntu 上的 bash 脚本中分隔命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23340268/

相关文章:

linux - Bash shell 脚本在脚本中获取用户的输入

bash - 如何在 bash 中使用表达式作为循环计数器?

bash - 在 bash 中将 stderr 复制到另一个文件时重定向 stdout 和 stderr

python - 使用pyenv在Python的两个系统版本之间切换

python - Ubuntu:无法为 python 设置 virtualenv

linux - tomcat7 localhost :8080 is not working?时怎么办

java - Java 中的 SSH 客户端和服务器

qt - 如何在 Qt 中轻松建立 SSH 连接?

bash - 使用ffmpeg按大小分割视频文件

bash - Github 权限被拒绝 : ssh add agent has no identities