[已解决]如何向root所属文件夹写入文件

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
xyy_xx
帖子: 344
注册时间: 2008-09-17 13:37
系统: arch|centos

[已解决]如何向root所属文件夹写入文件

#1

帖子 xyy_xx » 2012-04-29 20:37

如题
正在写一个bash脚本。需要想/etc下写入一个配置文件。
尝试过cp mv
sudo cp,sudo mv
以及在tmp下生成文件再把owner和group换成root后弄到etc 下也不行
求解
上次由 xyy_xx 在 2012-04-30 0:56,总共编辑 2 次。
拥有书籍并不表示拥有知识;拥有知识并不表示拥有技能;拥有技能并不表示拥有文化;拥有文化并不表示拥有智慧
渣浪:@CodewalkerDotMe
推特:@codewalkertse
博客:http://codewalker.me/
hanla
帖子: 5
注册时间: 2010-06-15 2:26

Re: 如何向root所属文件夹写入文件

#2

帖子 hanla » 2012-04-29 20:55

:em06 我也是小菜,不过我觉得应该是挂载/ 是以ro方式挂载的吧.remount 成rw,试一下.
头像
xyy_xx
帖子: 344
注册时间: 2008-09-17 13:37
系统: arch|centos

Re: 如何向root所属文件夹写入文件

#3

帖子 xyy_xx » 2012-04-29 21:06

hanla 写了::em06 我也是小菜,不过我觉得应该是挂载/ 是以ro方式挂载的吧.remount 成rw,试一下.
不好意思是etc这个目录,手拙敲快了。
谢谢各位了
上次由 xyy_xx 在 2012-04-30 0:43,总共编辑 1 次。
拥有书籍并不表示拥有知识;拥有知识并不表示拥有技能;拥有技能并不表示拥有文化;拥有文化并不表示拥有智慧
渣浪:@CodewalkerDotMe
推特:@codewalkertse
博客:http://codewalker.me/
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 如何向root所属文件夹写入文件

#4

帖子 lilydjwg » 2012-04-29 21:20

贴上脚本先。
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: 如何向root所属文件夹写入文件

#5

帖子 YeLee » 2012-04-29 21:37

No such file or directory
:em04 :em04 :em04 你懂的。
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
xyy_xx
帖子: 344
注册时间: 2008-09-17 13:37
系统: arch|centos

Re: 如何向root所属文件夹写入文件

#6

帖子 xyy_xx » 2012-04-29 23:40

lilydjwg 写了:贴上脚本先。

代码: 全选

    _net_config_file='/tmp/wireless-cfg';
    read -p "Your ESSID  ==>"cw
    if [ -f $_net_config_file ];then
        sudo echo '' > $_net_config_file;
    else
        sudo touch $_net_config_file;
    fi
    sudo echo "ESSID='$cw'" >> $_net_config_file;
    read -p "Please type your wireless_password==>    " wireless_password
    sudo echo "KEY='$wireless_password'" >> $_net_config_file;
    sudo echo "INTERFACE='wlan0'" >> $_net_config_file;
    sudo echo "IP='dhcp'" >> $_net_config_file;
    sudo echo "SECURITY='wpa'">> $_net_config_file;
    cat $_net_config_file;#测试输出一下内容
    sudo mv $_net_config_file /ect/network.d/wireless.cfg;
拥有书籍并不表示拥有知识;拥有知识并不表示拥有技能;拥有技能并不表示拥有文化;拥有文化并不表示拥有智慧
渣浪:@CodewalkerDotMe
推特:@codewalkertse
博客:http://codewalker.me/
头像
xyy_xx
帖子: 344
注册时间: 2008-09-17 13:37
系统: arch|centos

Re: 如何向root所属文件夹写入文件

#7

帖子 xyy_xx » 2012-04-29 23:42

YeLee 写了:
No such file or directory
:em04 :em04 :em04 你懂的。
因为移动不过去 也复制不过去不知道为何会这样。很简单的一个问题,不知道卡在哪了短路所以上来问问 :em04
拥有书籍并不表示拥有知识;拥有知识并不表示拥有技能;拥有技能并不表示拥有文化;拥有文化并不表示拥有智慧
渣浪:@CodewalkerDotMe
推特:@codewalkertse
博客:http://codewalker.me/
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 如何向root所属文件夹写入文件

#8

帖子 lilydjwg » 2012-04-30 0:19

xyy_xx 写了:
lilydjwg 写了:贴上脚本先。

代码: 全选

    _net_config_file='/tmp/wireless-cfg';
    read -p "Your ESSID  ==>"cw
    if [ -f $_net_config_file ];then
        sudo echo '' > $_net_config_file;
    else
        sudo touch $_net_config_file;
    fi
    sudo echo "ESSID='$cw'" >> $_net_config_file;
    read -p "Please type your wireless_password==>    " wireless_password
    sudo echo "KEY='$wireless_password'" >> $_net_config_file;
    sudo echo "INTERFACE='wlan0'" >> $_net_config_file;
    sudo echo "IP='dhcp'" >> $_net_config_file;
    sudo echo "SECURITY='wpa'">> $_net_config_file;
    cat $_net_config_file;#测试输出一下内容
    sudo mv $_net_config_file /ect/network.d/wireless.cfg;
1. 末尾不需要分号;
2. sudo 不是这么用的。sudo 用 root 权限执行了 echo 语句。但是,重定向是 shell 管的,而 shell 是普通用户权限。你可以用 sudo sh -c "echo xxx > yyy"。

另,为什么不不在脚本里使用 sudo,而让用户使用 sudo your_script 呢?
头像
xyy_xx
帖子: 344
注册时间: 2008-09-17 13:37
系统: arch|centos

Re: 如何向root所属文件夹写入文件

#9

帖子 xyy_xx » 2012-04-30 0:23

lilydjwg 写了:
xyy_xx 写了:
lilydjwg 写了:贴上脚本先。

代码: 全选

    _net_config_file='/tmp/wireless-cfg';
    read -p "Your ESSID  ==>"cw
    if [ -f $_net_config_file ];then
        sudo echo '' > $_net_config_file;
    else
        sudo touch $_net_config_file;
    fi
    sudo echo "ESSID='$cw'" >> $_net_config_file;
    read -p "Please type your wireless_password==>    " wireless_password
    sudo echo "KEY='$wireless_password'" >> $_net_config_file;
    sudo echo "INTERFACE='wlan0'" >> $_net_config_file;
    sudo echo "IP='dhcp'" >> $_net_config_file;
    sudo echo "SECURITY='wpa'">> $_net_config_file;
    cat $_net_config_file;#测试输出一下内容
    sudo mv $_net_config_file /ect/network.d/wireless.cfg;
1. 末尾不需要分号;
2. sudo 不是这么用的。sudo 用 root 权限执行了 echo 语句。但是,重定向是 shell 管的,而 shell 是普通用户权限。你可以用 sudo sh -c "echo xxx > yyy"。

另,为什么不不在脚本里使用 sudo,而让用户使用 sudo your_script 呢?
1写php习惯用;了
2脚本里面可以

代码: 全选

sudo sh -c "echo xxx > yyy"
3因为这个脚本里面还有其他的function是普通用户执行的不需要用到sudo
拥有书籍并不表示拥有知识;拥有知识并不表示拥有技能;拥有技能并不表示拥有文化;拥有文化并不表示拥有智慧
渣浪:@CodewalkerDotMe
推特:@codewalkertse
博客:http://codewalker.me/
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 如何向root所属文件夹写入文件

#10

帖子 lilydjwg » 2012-04-30 0:45

xyy_xx 写了: 3因为这个脚本里面还有其他的function是普通用户执行的不需要用到sudo
设计问题?
头像
xyy_xx
帖子: 344
注册时间: 2008-09-17 13:37
系统: arch|centos

Re: 如何向root所属文件夹写入文件

#11

帖子 xyy_xx » 2012-04-30 0:53

lilydjwg 写了:
xyy_xx 写了: 3因为这个脚本里面还有其他的function是普通用户执行的不需要用到sudo
设计问题?
嗯对,脚本有点儿乱,记录的是一些常用方法。通过参数来调用不同的方法。稍候空了还是分成不同的文件算了。
还是感谢你,写这么久脚本居然被路径给困这么久 晕死
拥有书籍并不表示拥有知识;拥有知识并不表示拥有技能;拥有技能并不表示拥有文化;拥有文化并不表示拥有智慧
渣浪:@CodewalkerDotMe
推特:@codewalkertse
博客:http://codewalker.me/
回复