admin管理员组文章数量:1026202
I am trying to write a shell script to check and delete files/folders
#!/bin/bash
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
find "$deletepath" -maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
I am having problems with 2 lines
stat -c "%y %n" "$dir" >> "$logpath"
For some reason $logpath
is not replaced and it says permission error.
and if condition does not work and always deleting files is printed. Help would be much appreciated.
I am trying to write a shell script to check and delete files/folders
#!/bin/bash
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
find "$deletepath" -maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
I am having problems with 2 lines
stat -c "%y %n" "$dir" >> "$logpath"
For some reason $logpath
is not replaced and it says permission error.
and if condition does not work and always deleting files is printed. Help would be much appreciated.
Share Improve this question edited Nov 19, 2024 at 9:50 oguz ismail 51.1k16 gold badges60 silver badges79 bronze badges asked Nov 18, 2024 at 13:59 HackerHacker 7,90620 gold badges90 silver badges168 bronze badges 7 | Show 2 more comments3 Answers
Reset to default 4As you're using bash you can optimize the code a litlle bit by not calling sh
at all:
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
while IFS= read -r -d '' dir
do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done < <(find "$deletepath" -maxdepth 5 -print0)
Both your problems have the same cause: You aren't passing the variables logpath
and donotdelete
into the copy of sh
that needs to use them.
Use export
to copy shell variables into the environment, where they'll be available to subprocesses, or self-assign them at the beginning of the line as follows to temporarily export them only for the find
command (and its subprocesses, thus also sh
):
# ''var=value somecommand'' exports var only for the duration of somecommand
logpath="$logpath" donotdelete="$donotdelete" find "$deletepath" \
-maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
With GNU find:
delete=
if test "$2" = false; then
delete=-delete
fi
find "$deletepath" -maxdepth 5 -printf '%T+ %TZ %p\n' >"$logpath" $delete
I am trying to write a shell script to check and delete files/folders
#!/bin/bash
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
find "$deletepath" -maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
I am having problems with 2 lines
stat -c "%y %n" "$dir" >> "$logpath"
For some reason $logpath
is not replaced and it says permission error.
and if condition does not work and always deleting files is printed. Help would be much appreciated.
I am trying to write a shell script to check and delete files/folders
#!/bin/bash
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
find "$deletepath" -maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
I am having problems with 2 lines
stat -c "%y %n" "$dir" >> "$logpath"
For some reason $logpath
is not replaced and it says permission error.
and if condition does not work and always deleting files is printed. Help would be much appreciated.
Share Improve this question edited Nov 19, 2024 at 9:50 oguz ismail 51.1k16 gold badges60 silver badges79 bronze badges asked Nov 18, 2024 at 13:59 HackerHacker 7,90620 gold badges90 silver badges168 bronze badges 7-
1
You need to
export logpath
for it to be visible in the subprocess. Same fordonotdelete
for that matter. – Charles Duffy Commented Nov 18, 2024 at 14:07 - (btw, it's curious to use bash for the main script and sh for the child process -- is that a deliberate decision? I'd generally expect that you'd want to use the same interpreter for both for ease-of-development). – Charles Duffy Commented Nov 18, 2024 at 14:09
- You mean use sh for script as well? – Hacker Commented Nov 18, 2024 at 14:14
- I mean, personally I tend to prefer to use bash everywhere, but you don't need it right now (unless you take Fravadona's suggestion, which makes use of bash-only features). But either way, picking one interpreter or the other and sticking to it makes more sense than using a different language variant for each part of your script. – Charles Duffy Commented Nov 18, 2024 at 14:15
- @Hacker Can you provide a sample directory tree and show what you want to delete from it? – Fravadona Commented Nov 19, 2024 at 10:28
3 Answers
Reset to default 4As you're using bash you can optimize the code a litlle bit by not calling sh
at all:
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
while IFS= read -r -d '' dir
do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done < <(find "$deletepath" -maxdepth 5 -print0)
Both your problems have the same cause: You aren't passing the variables logpath
and donotdelete
into the copy of sh
that needs to use them.
Use export
to copy shell variables into the environment, where they'll be available to subprocesses, or self-assign them at the beginning of the line as follows to temporarily export them only for the find
command (and its subprocesses, thus also sh
):
# ''var=value somecommand'' exports var only for the duration of somecommand
logpath="$logpath" donotdelete="$donotdelete" find "$deletepath" \
-maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
With GNU find:
delete=
if test "$2" = false; then
delete=-delete
fi
find "$deletepath" -maxdepth 5 -printf '%T+ %TZ %p\n' >"$logpath" $delete
本文标签: linuxfind exec sh c 3939 script not honoring variables set in outer scriptStack Overflow
版权声明:本文标题:linux - find -exec sh -c '...' script not honoring variables set in outer script - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612405a2159082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
export logpath
for it to be visible in the subprocess. Same fordonotdelete
for that matter. – Charles Duffy Commented Nov 18, 2024 at 14:07