Shell operations from C++


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell operations from C++
# 1  
Old 05-07-2014
Shell operations from C++

Hi everyone,

I need little help in shell operations from C++ program.
Here I furnish the details of problem:
1. Lets say my current working path is myWorkingPath.
2. In my working path I have list of name directories and each name directory has two more sub directories say A/B.
(now path to B will be pathB = myWorkingPath/name/A/B)
3. I have a executable (say run) in myWorkingPath directory, that i wanted to execute from B directory.
4. From C++ program I can do shell operations like mkdir, rm etc using system("mkdir NewDir"), system("rm file") etc., (these shell operations will done form myWorkingPath)
but unable to do some shell operations from pathB. How can I communicate pathB for system() operations?

lets say I have saved this path in pathB="/home/linuxUser/myWorkingPath/name/A/B". here name is a variable.

Code:
for(i=0;i<nameList.size(); i++){
pathB="/home/linuxUser/myWorkingPath"/nameList.name(i)/"A/B";
system("cd pathB"); // to goto that B directory
system("../../../run"); // to run the executable
}

I know this program is wrong but, may give you clear idea what i wanted to do.

can any one help me?

Regards,
linuxUser_

Last edited by linuxUser_; 05-07-2014 at 05:07 PM..
# 2  
Old 05-07-2014
Why do you need to cd? Just give the full path to the second executable you want to run.
# 3  
Old 05-07-2014
Quote:
Originally Posted by blackrageous
Why do you need to cd? Just give the full path to the second executable you want to run.
cd is just to convey that my script file run is written to execute from pathB.
so to make corresponding changes in pathB, one should go to that path and run the executable.

let say i am in working Directory.
to goto pathB
Code:
cd name/A/B

now i am in pathB and wanted to execute run which is in working dir
Code:
../../../run

hope it will make clear.

Regards,
linuxUser_
# 4  
Old 05-07-2014
You could also use (inside script):
Code:
cd "${0/${0##/*/}}"

Or for better readability:
Code:
cd "$(dirname $0)"

hth

EDIT2:
How do you "rm file", just the file or the full path?
If full path, why wont that work?
If not full path, what is what you actualy want to achieve?

Last edited by sea; 05-07-2014 at 10:00 PM..
# 5  
Old 05-08-2014
problem is in my c++ program i have saved a path that will vary with time (path referred as pathB in above description). now if i do some shell operations through c++ like system("rm $pathB filename") it will not work because shell dont know pathB.
question is how can i communicate shell about pathB that vary with time?

Regards,
linuxUser_
# 6  
Old 05-08-2014
Probably better to have it as an environmental variable then, or passed in as a parameter so you keep your code flexible and don't have to keep editing it. With change comes risk.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Operations

Hi Folks, Below is example of an Input data which is used, based on the last 2, 3 & 4 column, I want my first column data to be collated as shown in the output section. a,ac,tc,ic b,ac,tc,ic c,ac,tc,ic d,ac,tc,ic b,bc,tc,ic d,bc,tc,ic e,bc,tc,ic I want my output to be ... (2 Replies)
Discussion started by: nikhil jain
2 Replies

2. Shell Programming and Scripting

To skip operations in UNIX shell

hi i am having a acript for which i need to skip the execution of some lines and to continue with remaining lines for eg script.sh rm text for i in * do . . . . . if then rm i want to skip the execution of the lines and to start with (11 Replies)
Discussion started by: rohit_shinez
11 Replies

3. Shell Programming and Scripting

Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file i want to multiply this input with value .43, and assign it to variable b. i tried it as below: #!/bin/sh a=$1 b=`expr $1\*0.43` echo b=$b error : expr: non-integer argument Please tell me , how to do this. Thanks (10 Replies)
Discussion started by: rishifrnds
10 Replies

4. Programming

shell cursor operations

Hi I need to save the actual cursor position into variable in my script. How can I do it ? thx for help. (1 Reply)
Discussion started by: presul
1 Replies

5. Programming

shell scripting problems involving operations with remote machine

Hi, i have been developing a shell script to transfer a set of files from one ubuntu system to another. Task: while executing the script the files ( ls, dir, cat) in the source machine should transfer to destination machine(at /home/mac/mac/bin) While the script is executed once again, It... (0 Replies)
Discussion started by: srijith
0 Replies

6. Shell Programming and Scripting

Help on shell script (string operations)

Hey everyone. So the background of the problem is that the ps3 does not support the mkv container, but DOES support the avi one. Here is the script to convert one file with the name hardcoded in: #!/bin/sh mencoder -oac... (2 Replies)
Discussion started by: wua05
2 Replies

7. Shell Programming and Scripting

file operations in shell scripting

hi All, my query... 1.I Have to search for the files in the root directory. 2.i have to search for a pattern in all the files in the root directory and then replace them with a new pattern. 3.Rename the file Explanation: if ABC is the root folder and has 3 subfolders and there are 15... (9 Replies)
Discussion started by: adityamahi
9 Replies

8. Shell Programming and Scripting

Unix file operations(shell script)

Hi, I want to compare two files. Files will look like as follows: file1: ASDFGHJU|1234567890123456 QWERTYUI|3456789098900890 file2: ZXCVBVNM|0987654321234567 POLKIJUYH|1234789060985478 output file should be: ASDFGHJU|1234567890123456 QWERTYUI|3456789098900890 Thnaks in advance (6 Replies)
Discussion started by: nivas
6 Replies

9. UNIX for Dummies Questions & Answers

File operations

Hi I have a tab delimited file with 3 fields. I need to sort this file on the first field and remove all the records where the first field has dulplicates. For eg my file is 133|arrfdfdg|sdfdsg 234|asfsdgfs|aasdfs 133|affbfsde|dgfg When this file gets sorted I need the result to be ... (2 Replies)
Discussion started by: monks
2 Replies
Login or Register to Ask a Question