system() regular expression

Hi all :

I want to del all .tmp files in my /tmp/ , so I use system("rm -f /tmp/.tmp");
but It's wrong , I must pass a path without regular expressions . eg:

system("rm -f /tmp/1.txt")

the question is I don't know the exactly files name so I can't del it as above , but I know it's filetype or suffix (.tmp )

Best regards
aobai

The system function does expand its parameters so there is nothing specific to do. Your example will work if you tell what to do with the files which you fail to.

I see you , but I just know its suffix . not its full name :confused:

thank you !!

That's fine. Knowing its suffix is enough. Please post the code you are testing. The previous one is obviously incomplete as I already wrote.

what about

ls /tmp/*.tmp

and if this is correct then

rm /tmp/*.tmp

system("rm -f `ls /tmp/*.tmp`")

You don't need the use the ls command

system("rm -f /tmp/*.tmp")

Jean-Pierre.

Hii aobai,

Try it below command.

rm -rfv `find -iname *.tmp`[COLOR="\#738fbf"]

rm -rfv `find -iname *.tmp`

./system /tmp *.tmp
#!/bin/bash
 
myworkdir=$1
files="$2"
 
   if [ "$myworkdir" == "" -o "$files" == "" ] ; then
       echo "Usage: $0 "directory" "files" [ex : $0 /tmp "\"tmp*"\" ] "
        exit 1
   fi
echo "Listing Files"
sleep 1
cd $myworkdir
ls -l $files 2>/dev/null
if [ $? -ne 0 ] ; then
    echo -e "No File Deleted..\nExiting.."
  exit 1
fi
read -p "Are you sure deleted files..[y/n]" st
 
  if [ $st == "y" ] ; then
     find $myworkdir -name "$files" -exec rm -f {} \;
       if [ $? -ne 0 ] ; then
           echo "No File Deleted.."
         else
             echo "Listed Files Deleted.."
          exit 1
      fi
  fi

Looks like you guys failed to understand the OP want C code, not shell scripts.

system is a function available within AWK

Jean-Pierre.

Thanks Jean-Pierre for opening my eyes !
I overlook this is the shell programming and scripting forum :o

In any case, awk "system()" behave the same way as the C standard library one, i.e. it expands its parameters. Too bad aobai provides no more clues ...

There is nothing wrong with the code in the original post. If it's not working, please be clear about how it is failing. Post any error messages if any. Also, try enabling tracing in the shell that's being used:

system("set -x; rm -f /tmp/*.tmp");

Also, please post the entire program, or a minimized version that exhibits the same problem. Perhaps the error is in the surrounding code. Although, we can't even guess because not only have you not specified the error at all, there is even some doubt as to what programming language is being used.

Assuming that it's AWK, here's a test run showing how globs work just fine with the system function:

system.awk

BEGIN {
    system("touch 1.txt 2.txt 3.txt");
    system("ls *.txt");
    system("set -x; rm -f *.txt");
    system("ls *.txt");
}
$ awk -f system.awk 
1.txt   2.txt   3.txt
+ rm -f 1.txt 2.txt 3.txt
ls: *.txt: No such file or directory

The first line is ls showing us that the files were created successfully by touch. The second line is the trace output from 'set -x', showing the rm command that will run (as you can see, the .txt pattern was expanded correctly). The final line is ls complaining that nothing matched '.txt', which is expected since in that empty directory, there is nothing for the pattern to match and no file matches the unexpanded string '*.txt'.

Regards,
Alister

@aobai

What Operating System do you have?
What programming language or scripting language are you using?
What precisely are you trying to achieve?

Running a command like this (by whatever means) on a live system is idiotic.

Please state exactly what your tutor has asked you to write and any time parameters in relation to age of files.