LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-27-2011, 05:10 PM   #1
_Chops_
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
Question shell commands in c++


I am pretty new to programming and working with terminal. I am trying to create a program that will do a few things when i open terminal. this is the code i have so far


#include <unistd.h>

int main()
{
system("say -v zarvox bears beats battlestar galactica");
system("/Users/jarrodcoffin/prog/c/Basic/mine");
chdir("/Users/my_username/School/C++/template/template");

return 0;
}

the first two system functions work just fine. When I get to the "chdir()" it doesn't change the directory in the shell window that I am working in. Im sort of confused as to why it won't work. If someone is kind enough to explain why its not working, I would be extremely appreciative.

As a side note: this isn't for a school assignment, so I'm not cheating or anything.
 
Old 09-27-2011, 05:32 PM   #2
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Why do you need to use a C program for this (you're not using any C++ features here; this is pure C)? Can't you just use .bashrc or .profile if you want to run these commands whenever you open a shell/terminal? (…or whatever gets parsed/run whenever you open a terminal on OS X; I've never done anything remotely "technical/geeky" on a Mac, so I wouldn't know for sure. )

Typically, when you want to run several shell commands in sequence, you use a shell script rather than a C/C++ program.

A suggested replacement:

Code:
#!/bin/bash

say -v zarvox bears beats battlestar galactica
/Users/jarrodcoffin/prog/c/Basic/mine
cd /Users/my_username/School/C++/template/template
…have this called from .bashrc or .profile or whatever it is on OS X, or just put the commands in one of those directly (without the shebang line at the top).

I apologize if you already know this and are simply trying to deliberately run shell commands from a C program for practice.
 
Old 09-27-2011, 05:35 PM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by _Chops_ View Post
When I get to the "chdir()" it doesn't change the directory in the shell window that I am working in. Im sort of confused as to why it won't work. If someone is kind enough to explain why its not working, I would be extremely appreciative.
chdir() only changes the working directory for the C program (you can see that by running pwd from within the C program), not for the shell environment you run the program from.
 
Old 09-27-2011, 07:17 PM   #4
_Chops_
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
thank you mr code and mill gates. So I guess what I'm looking for is something that will change the environment I'm running the program in. It doesn't necessarily have to be in "C". the way i was doing it before bugged me so i wanted a different way. thanks for the suggestions mr. code. I'll try that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
more shell commands mf5160 Linux - Software 3 03-14-2007 01:41 PM
about shell commands nesta Linux - Newbie 3 12-12-2006 05:40 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
commands in shell? kalleanka Linux - General 9 02-01-2006 07:38 AM
shell commands! krishlinux General 1 10-05-2003 04:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration