0

I have created a script that takes a file(FiletoModify.txt) and copy line by line everything it contains to a new txt file which I called it output.txt.My problem is that I want to call system to do the following in my code but it doesn't work.I'm a new in C programming and I'm making it clear that I can't see my mistake and I really need some help.Thank you

My code in my C program:

system("cd Desktop; chmod +x script.sh ; ./script.sh");

Here's the script code:

    #!/bin/bash
    while IFS= read -r line
    do
    echo "$line"
    echo -e "$line\n" >>output.txt

    done <"FiletoModify"
12
  • Even if it otherwise worked, this wouldn't copy your file faithfully -- it would convert \t to a tab character, for instance, or \n to an extra newline. Don't use echo -e (which is also non-POSIX); printf '%s\n' "$line" is your friend. Commented Dec 29, 2014 at 23:55
  • 2
    Beyond that... well, why use system() at all? You can call chdir(), chmod(), etc., from C -- which is faster and doesn't open you up to bugs like shellshock. Commented Dec 29, 2014 at 23:56
  • I only know the system function.I have also stored this script in my desktop.What else do you suggest me to do?.....If I use chmod() how can I pass the arguments? Commented Dec 29, 2014 at 23:57
  • Also, is the // in your original code? For a shebang to work, the #! need to be the literal first two characters of the file; you can't have anything else before them. Commented Dec 29, 2014 at 23:58
  • 1
    What is the actual error message that you're getting? Commented Dec 30, 2014 at 0:06

1 Answer 1

1

Okay, so from your error message your problem is with the cd Desktop; part of your C program. Two possible fixes:

  1. Delete cd Desktop; from your system call.

  2. Run your C executable from a directory one level above Desktop (i.e. the directory which contains Desktop).

Sign up to request clarification or add additional context in comments.

6 Comments

sh: 1: ./script.sh: not found I did it but this is what a got from terminal!
My previous comment was incorrect. Assuming that you're now running your C executable from one level above Desktop, make sure that there actually is a shell script named script.sh in your Desktop directory. The error you're getting would indicate that currently there isn't.
Always believe your error messages. Try to address the problem they describe (they're usually accurate, if a bit stupid and hard to understand). Even if your program doesn't run, if your error message changes consider this to be progress.
I have saved everything in Desktop but I haven't found a solution yet
Your link points to my dropbox. You'll need to right click on your code and select "Copy public link" in order to get the correct link.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.