linux - find -exec sh -c '...' script not honoring variables set in outer script - Stack Overflow

I am trying to write a shell script to check and delete filesfolders#!binbashdeletepath="path&

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 for donotdelete 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
 |  Show 2 more comments

3 Answers 3

Reset to default 4

As 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745612406a4636027.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信