gpt4 book ai didi

linux - 为什么我不断收到 "cp: cannot stat ' ' : No such file or directory"or "cp: missing opereand" in my bash script

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:56 29 4
gpt4 key购买 nike

我正在编写一个脚本来设置 debian 安装,这个错误让我很沮丧:

问题:

cp "$BASHRC" "$HOME"/.bashrc

给出:

+ sudo -u billy bash

cp: cannot stat '': No such file or directory

or

cp: missing opereand

这是脚本:

这些文件在脚本所在的当前目录中...

 17 BASHRC=.bashrc
18 NANORC=.nanorc
19 BASHRCROOT=.bashrcroot

#!/bin/bash -x
2
3 SCRIPTNAME=`basename "$0"`
4
5 if [ "$#" -eq 0 ]
6 then
7 echo "No arguments supplied"
8 echo "Usage: $SCRIPTNAME user1name user2name\(optional\) user3name\(optional\)"
9 fi
10
11 echo "Here starts the party, "
12 echo "This program should be run as root, please wait"
13 echo "Setting up server.........."
14
15 DIRBASHRCROOT="$HOME"/.bashrcroot
16 DIRBASHRC="$HOME"/.bashrc
17 BASHRC=.bashrc
18 NANORC=.nanorc
19 BASHRCROOT=.bashrcroot
20 ROOT=root
21 USER1="$1"
22 USER2="$2"
23 USER3="$3"
24
25 ################ Users and access settings #####################
26 #echo username1 ALL=(username2) NOPASSWD: /bin/bash /path/to/svn >> /etc/sudoers
27 #echo `users` | grep "$1" && echo User $1 exists
28 #sudo -u username2 -H sh -c "cd /home/$USERNAME/$PROJECT; svn update"
29 #useradd -m -s /bin/bash "$different_user"
30 #id -u $USER1
31 checkIfUser()
32 {
33 for name in "$@"
34 do
35 if id -u "$name" #>/dev/null 2>&1
36 then
37 echo 'User: "$name" exists....setting up now\!'
38 else
39 echo 'User: "$name" does not exists....creating now\!'
40 useradd -m -s /bin/bash "$name" #>/dev/null 2>&1
41 fi
42 done
43 }
44 checkIfUser $1 $2 $3
45 ################ NANO SYNTAX-HIGHLIGHTING #####################3
46 sleep 3
47
48
49
50
51
52 if [ "$UID" != 0 ]
53 then
54 sudo -u "$ROOT" bash <<'EOF'
55 sleep 5
56 git clone https://github.com/nanorc/nanorc.git
57 sleep 5
58 cd nanorc
59 make install-global
60 sleep 5
61 cp "$NANORC" /etc/nanorc
62
63 if [ "$?" = 0 ]
64 then
65 echo "Implementing a custom nanorc file succeeded\!"
66 fi
67EOF
68 else
69 git clone https://github.com/nanorc/nanorc.git
70 sleep 5
71 cd nanorc
72 sleep 5
73 make install-global
74 sleep 5
75 cp "$NANORC" /etc/nanorc
76
77 if [ "$?" = 0 ]
78 then
79 echo "Implementing a custom nanorc file succeeded\!"
80 fi
81 fi
82
83 echo "Finished setting up nano\!"
84 ################ LS_COLORS SETTINGS #############################

86 if [ "$UID" != 0 ]
87 then
88 sudo -u "$ROOT" bash <<'EOF'
89 cp "$BASHRCROOT" "$HOME"/.bashrc
90 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
91 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
92 . "$HOME"/.bashrc
93 echo "Here is LS_COLORS in action: "
94 ls -l "$HOME"/
95 EOF
96 else
97 cp "$BASHRCROOT" $(eval echo "$HOME"/.bashrc)
98 sleep 5
99 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
100 sleep 5
101 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
102 sleep 5
103 . "$HOME"/.bashrc
104 echo "Here is LS_COLORS in action: "
105 sleep 5
106 ls -l "$HOME"/
107 fi
108 if [ ! -z "$USER1" ]
109 then
110 sudo -u "$USER1" bash <<'EOF'
111 sleep 5


THESE GUY'S ARE THE CULPRIT↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
112 cp "$BASHRC" "$HOME"/.bashrc
HERE IS THE PROBLEM ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
113 sleep 5
114 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
sleep 5
116 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
117 . "$HOME"/.bashrc
118 sleep 5
119 echo "Here is LS_COLORS in action: "
120 ls -l "$HOME"/
121 EOF
122 fi
123
124 if [ ! -z "$USER2" ]
125 then
126 sudo -u "$USER2" bash <<'EOF'
127 cp "$BASHRC" "$HOME"/.bashrc
128 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
129 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
130 . "$HOME"/.bashrc
131 echo "Here is LS_COLORS in action: "
132 ls -l "$HOME"/
133 EOF
134 fi
135
136 if [ ! -z "$USER3" ]
137 then
138 sudo -u "$USER3" bash <<'EOF'
139 sleep 5
140 cp "$BASHRC" "$HOME"/.bashrc
141 sleep 5
142 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
sleep 5
142 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
143 sleep 5
144 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
145 sleep 5
146 . "$HOME"/.bashrc
147 echo "Here is LS_COLORS in action: "
148 ls -l "$HOME"/
149 EOF
150 fi
151 echo "Finished setting up LS_COLORS on your files and directories\!"

问题:

这似乎是什么问题,最佳实践和解决方案是什么?

最佳答案

您正在通过 sudo 启动另一个 bash。 BASHRC 是局部变量,子进程不可见。

由于 sudo 会出现另一个问题:如果/etc/sudoers 包含选项 Defaults env_reset,那么 sudo 将丢弃几乎所有环境变量(PATH 除外)。

如果将 <<'EOF' 更改为 << EOF,您可以控制哪些变量将在 sudo 之前扩展,哪些在 sudo 之后扩展:

#!/bin/sh
EARLY_EXPANSION=foo
export LATER_EXPANSION=bar

bash <<EOF
echo "$EARLY_EXPANSION \$LATER_EXPANSION"
sleep 5
echo $(date) \$(date)
EOF

关于linux - 为什么我不断收到 "cp: cannot stat ' ' : No such file or directory"or "cp: missing opereand" in my bash script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51905689/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com