07-07-2015 05:22 PM
When we use this code to delete a folder :
call system(" rm -rf DIR1/DIR2/&folder1");
If sas does not find the macro variable &folder1, it will delete the directory : DIR1, why ?
Is there any solution to avoid the problem.
07-07-2015 05:27 PM
SAS doesn't do anything but pass a command to the system, you need to ensure the command is what you want.
If &folder1 doesn't exist it becomes:
rm -rf DIR1/DIR2/
-r, -R, --recursive remove the contents of directories recursively (the force option must often be used to successfully run rm recursively)
-f, --force ignore nonexistent files, never prompt
Find the correct unix command that generates the result you want.
07-07-2015 07:03 PM
no, but i want to know , if there is "an option or idea " to avoid the option -rf , if it can not find the macro : variable : &folder1.
Now, if it can not find the &folder, it will delete : DIR2; it is really a problem!!!
07-08-2015 01:51 AM
The Unix "rm -rf ..." will delete all what is in that path. The way of your coding is resulting in possible unwanted sideeffects deleting the wrong things.
The only protections you have for unintended sideeffects are:
- OS security
- Defensive in coding.
Defensive coding with delering in your example.
Position the current dir in a location you do not want to delete but only some members, Than delete those.
As you have a position current directory being set you are working relative to that one. You could be more trustworthy by an absolute path.
For better traceability I would use piping instead of just system calls. The "2>1" will let show up possible error messages in your sas log with all common output of all commands. The "cd " command should work fine when not de deletion of only &folder is attempted I a wrong location. When folder is wrong it will try to delete just something in you current dir. Imagine the rootfolder is your current dir and you will delete all below that (rm -rf *). All files you have access to will be deleted. It are Unix commands that will cause that not SAS.
filename system "cd DIR1/DIR2 ; rm -rf &folder1 2>1" ;
data _null_ ;
infile system ; input ; put _infile_ ;
07-08-2015 03:52 PM
Thank you for your message.
Imagine that, we can forget to set this check if "&folder1 " ne " " then
is not an option for user or administraror to stop the run of "rm" in the synthax :call system("rm -rf DIR1/DIR2/&folder1")
07-10-2015 11:32 AM
Tom just coding that command a little bit different (first a cd) already achieved that at the coding level.
Setting OS controls (chmod) for restricted access is another sensible approach. As is promoted by SAS everything to run by sassrv (and sometimes) also installed by that there is een OS security challenge to be solved. Having the x-cmd discussions is a result of this bas SAS approach. Would the process run in a security setting DIR1 and DIR2 never could hurt as of your proposal is also the proposal of correcting that sas default.