echo to file - error: cannot create
echo to file - error: cannot create
在 Unix 中 ksh。我正在尝试使用下面的 echo 语句
创建一个包含几行文本的文本文件
echo $multilinetext > ../in/log_file
我收到的错误是 "Cannot Create"。这段代码在一段时间之前也能正常工作。
我可以通过 filezilla 创建文件。 "in" 目录拥有所有权限(777)。
Dir 层次结构如下
ParentDir
in
Bin
test.ksh
test.ksh正在执行回显命令
还有什么可能导致错误
更新:
问题是我从 parentDir 执行了脚本。所以它没有说文件不存在,而是在Bin中找到了test.ksh文件并执行,没有任何错误。这导致它在错误的位置寻找 "in" 目录
是否可以检查脚本是否从其自己的目录执行?
根据您的要求,这是一个基于您发布的有关层次结构的小脚本:
#!/bin/sh -
# adjust this to suit
bin_dir="/tmp/top/bin"
multi=`cat /etc/passwd`
# get current directory
pwd="`pwd`"
# check to ensure we're in the correct directory
if [ "$pwd" != "$bin_dir" ]; then
echo "not executing from correct bin directory"
exit
fi
target_dir="../in"
# ensure the target directory for writing exists
if [ -d "$target_dir" ]; then
echo $multi > $target_dir/log_file
else
echo "unable to locate directory '$target_dir' -- does not exist"
fi
在 Unix 中 ksh。我正在尝试使用下面的 echo 语句
创建一个包含几行文本的文本文件echo $multilinetext > ../in/log_file
我收到的错误是 "Cannot Create"。这段代码在一段时间之前也能正常工作。
我可以通过 filezilla 创建文件。 "in" 目录拥有所有权限(777)。 Dir 层次结构如下
ParentDir
in
Bin
test.ksh
test.ksh正在执行回显命令
还有什么可能导致错误
更新: 问题是我从 parentDir 执行了脚本。所以它没有说文件不存在,而是在Bin中找到了test.ksh文件并执行,没有任何错误。这导致它在错误的位置寻找 "in" 目录
是否可以检查脚本是否从其自己的目录执行?
根据您的要求,这是一个基于您发布的有关层次结构的小脚本:
#!/bin/sh -
# adjust this to suit
bin_dir="/tmp/top/bin"
multi=`cat /etc/passwd`
# get current directory
pwd="`pwd`"
# check to ensure we're in the correct directory
if [ "$pwd" != "$bin_dir" ]; then
echo "not executing from correct bin directory"
exit
fi
target_dir="../in"
# ensure the target directory for writing exists
if [ -d "$target_dir" ]; then
echo $multi > $target_dir/log_file
else
echo "unable to locate directory '$target_dir' -- does not exist"
fi