Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Current Directory 3

Status
Not open for further replies.

Jarrod13

Programmer
Dec 18, 2008
12
I'm trying to use the system command to change the current directory:

My root directory is c:\qa1.0, When I run the following code I'm setting the DIRECTORY variable to "c:\qa1.0\Pass"

The system command is not recognizing the DIRECTORY variable.

Code:
print("Please enter the directory you wish to change to:");
   getline DIRECTORY < "-";
   print(DIRECTORY);
   system("cd " DIRECTORY);
   print("This is the current directory: ");
   system("chdir");

When DIRECTORY is printed out, the correct path is printed however when chdir is run it displays "c:\qa1.0" (NOT c:\qa1.0\Pass)


When I type in the manual path into the system function:
Code:
system("cd c:\qa1.0\Pass")
it works correctly.

Any ideas?
 
Hi Jarrod,

I stumbled across the same problem; there seems to be something funny about the system() command in some versions of awk where it doesn't handle strings the same way as everywhere else. The workaround I have used is to simply assign the whole command to a variable, e.g.

Code:
  print(DIRECTORY);
        COMMAND="cd " DIRETORY;
        system(COMMAND);

I consider this to be a bug, maybe someone else knows otherwise. What version of awk and OS are you using?


Annihilannic.
 
Annihilannic,

Thanks for the work around. I'm happy accepting it as a bug as well. I'm using AWK version 3.1.6 w/ XP (cygwin library)

J

 
Hi

I still not understand. The [tt]system()[/tt] function starts a new shell instance to execute the given command. That sub-process lives only as long as the [tt]system()[/tt] function runs. So you have :
Code:
system("cd " DIRECTORY);  [gray]# <- sub-process 1[/gray]
print("This is the current directory: ");
system("chdir");          [gray]# <- sub-process 2[/gray]
So I would say, the [tt]cd[/tt] command works, but its effect vanishes long time before you are checking for it. It should work only if you execute the [tt]cd[/tt] and the [tt]chdir[/tt] ( or [tt]pwd[/tt] on POSIX systems ) in the same sub-process, so a single [tt]system()[/tt] call :
Code:
print("Please enter the directory you wish to change to:")
getline DIRECTORY < "-"
print(DIRECTORY)
system("cd " DIRECTORY"; echo This is the current directory:; pwd")
Honestly, I do not understand what Annihilannic's suggestion could change.


Feherke.
 
You're absolutely correct feherke, I wasn't thinking about the command in use here, just focussing on the fact that sometimes string concatentation in the parameters to system() function calls has no effect.

I'm pretty sure it works fine in GNU awk, so I should have realised my mistake when the OP said that's what they were using...

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top