最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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
Post a comment

comment list (0)

  1. No comments so far