LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2014, 07:54 AM   #1
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Rep: Reputation: Disabled
How to write "#include <stdlib.h>" to many c files with shell script


Now I have many(about 200) c files like this:

// hello.c

#include <stdio.h>

main()
{
printf("hello, world\n");

exit(0);
}

and I want to add "#include <stdlib.h>" to these files

and I try many times but can not succed

can you give me some advice ?


I am sorry ,my "Wrap [CODE]" can not use
 
Old 09-16-2014, 08:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,925

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
how did you try that?
 
Old 09-16-2014, 08:15 AM   #3
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
how did you try that?


I read the c file and try to write "#include <stdlib.h>" before the function "main()"
 
Old 09-16-2014, 09:23 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
this could the the core of a cycle:

Code:
...
mv "$FILE" "$FILE.before"
sed 's|^#include <stdio.h>$|#include <stdio.h>\n#include <stdlib.h>|' "$FILE.before" >"$FILE"
...
 
Old 09-16-2014, 11:25 AM   #5
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Original Poster
Rep: Reputation: Disabled
mm

Quote:
Originally Posted by NevemTeve View Post
this could the the core of a cycle:

Code:
...
mv "$FILE" "$FILE.before"
sed 's|^#include <stdio.h>$|#include <stdio.h>\n#include <stdlib.h>|' "$FILE.before" >"$FILE"
...

sed 's|^#include <stdio.h>$|#include <stdio.h>\n#include <stdlib.h>|' "$FILE.before" >"$FILE"

this is a very good suggestion.thanks a lot!

i am not very familiar with the tool sed, you help me a lot!


by the way ,do you konw why my "Wrap [CODE]" could not use?
 
Old 09-16-2014, 12:22 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Only type [code] before your code, and [/code] after it. It's easy, just practice diligently.
 
Old 09-17-2014, 03:05 AM   #7
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Only type [code] before your code, and [/code] after it. It's easy, just practice diligently.



OK! thanks!

---------- Post added 09-17-14 at 03:05 AM ----------

Code:

mmmm


mmm
 
Old 09-17-2014, 02:03 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
There's one thing about learning techniques for editing multiple files via sed or awk, but don't just blindly add stdlib.h to every file because you "think" you need it. I'm a proponent of not adding include files unless I employ library calls which require those include files. Otherwise one perpetuates this highly lengthy list of include files when they don't really know or care whether or not the list is right, if there are replacements of definitions, or whether or not much of the includes are required. Many's the time I've encountered a lengthy list of includes, some of them standard library ones, but in truth many of them locally defined ones for a particular coding project; where you end up having something defined in duplicate and therefore re-defined to a value you didn't expect. I've seen an include file which checked whether or not a value was defined, performed an undefine action and then forced that value to be what the coder wished. In the end it was "odd" but not destructive; however it made us wary that two or more coders were using the same designations; they were different, and one would win if a particular include file was used. Meanwhile if the other include file was solely used, that definition would still exist, but be a different declaration. As I say, it was not a real problem, but we saw potential there for it to be.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write "#include<stdlib.h>" to many c files using shell dy20082250 Programming 1 09-16-2014 08:08 AM
"How to rename files with current date stamp appended into name using shell script?" nandi Linux - Newbie 4 10-05-2011 10:36 AM
How to write shell script that adds an extension ".new" to all the files in a direct ayongying Programming 2 05-28-2010 10:09 AM
How to write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 12:17 PM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 04:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

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