SAS and unix

Reply
Super Contributor
Posts: 371

SAS and unix

When we use this code to delete a folder :

folder1

data _null_;

call system(" rm -rf   DIR1/DIR2/&folder1");

run

If sas does not find the macro variable &folder1, it will delete the directory : DIR1, why ?

Is there any solution to avoid the problem.

Thank you

Grand Advisor
Posts: 17,396

Re: SAS and unix

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.

Super Contributor
Posts: 371

Re: SAS and unix

My code deletes the folder : DIR2. if it does not find  the macro variable &folder1 I do not want that, I want to avoid

Grand Advisor
Posts: 17,396

Re: SAS and unix

Have you tried removing the recursive option?

Super Contributor
Posts: 371

Re: SAS and unix

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!!!

Grand Advisor
Posts: 17,396

Re: SAS and unix

Conditionally execute - check if macro variable exist and if it doesn't then don't execute the code.

Super User
Super User
Posts: 6,364

Re: SAS and unix

Since you are running it in a data step why not just test before executing?

data _null_;

  if "&folder1 " ne " " then

    call system("rm -rf   DIR1/DIR2/&folder1")

  ;

run;

Valued Guide
Posts: 3,206

Re: SAS and unix

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_ ;

run;

---->-- ja karman --<-----
Super Contributor
Posts: 371

Re: SAS and unix

Thank you for your message.

You're right.

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")

Grand Advisor
Posts: 17,396

Re: SAS and unix

I'm not sure I understand. Do you have the solution to your question? If so please mark the question answered, otherwise please clarify.

Super Contributor
Posts: 371

Re: SAS and unix

No, I need usually  a solution.

As I said, I want sas to stop the   running of "rm", if I have a macro variable null, even if I have not written  the condition if "&folder1 " ne " "

Grand Advisor
Posts: 17,396

Re: SAS and unix

So you can't change the unix code and can't add in a conditional execution either?

Super User
Super User
Posts: 6,364

Re: SAS and unix

What you want sounds impossible.

Could you use operating system commands to change the permissions on the DIR1 and/or DIR2 folders to make it impossible for the rm command to remove them?

Valued Guide
Posts: 3,206

Re: SAS and unix

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.     

---->-- ja karman --<-----
Super Contributor
Posts: 371

Re: SAS and unix

Yes, You can have some users they do not know the matter, that's why, i am intersted by the solution to stop the unix command :rm

Ask a Question
Discussion stats
  • 33 replies
  • 676 views
  • 7 likes
  • 7 in conversation