Copy content of multiple sh's into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy content of multiple sh's into one
# 1  
Old 05-09-2013
Copy content of multiple sh's into one

Hi,

I got the following problem.

I got a folder called "renapple" whose content has multiple ".sh" files in it:

Quote:
test1.app.sh
quoka.app.sh
quite.app.sh
....
they all contain one line of text: (this is just an example, the content itself looks different).
Code:
cp path1/file path2

What I want to do is:
- copy the content of all those files into new .sh file.
However every previous sh file should get its own newlin.

so the final sh should look a little bit something like this

Code:
cp path1/file path2
cp path1/file path2
cp path1/file path2
cp path1/file path2

Thanks in advance for any pointers.
# 2  
Old 05-09-2013
Like this?
Code:
cat *.sh > final.sh

# 3  
Old 05-09-2013
Thats a simple but effective solution. Thanks.

Will this also work with characters such as ä ü ä ß etc. ?
# 4  
Old 05-09-2013
Quote:
Originally Posted by verdepollo
Like this?
Code:
cat *.sh > final.sh

Unless you are guaranteed that final.sh will sort 1st in the list of files selected by *.sh (after final.sh is created), this cat will either duplicate some lines in final.sh or go into a loop repeatedly copying some lines from the start of final.sh into final.sh until the filesystem runs out of space or you hit the maximum file size limit for final.sh.

To perform this cat safely, the output file has to have a name that won't be matched by the pattern that is being given as an operand to cat. This can be done several ways; two include by directing the output to a file in another directory, as in:
Code:
cat *.sh > ../final.sh

or by using a different filename suffix for the target, as in:
Code:
cat *.sh > final$$ && mv final$$ final.sh

Any of these will work with any filenames ending in .sh.
# 5  
Old 05-10-2013
Quote:
Originally Posted by Don Cragun
Unless you are guaranteed that final.sh will sort 1st in the list of files selected by *.sh (after final.sh is created), this cat will either duplicate some lines in final.sh or go into a loop
KSH and Bash expansions are guaranteed to create an alphabetically sorted list of names before "feeding" it to cat.

Besides, file name expansion is performed before any redirection occurs.

Not sure about other shells.
# 6  
Old 05-10-2013
Quote:
Originally Posted by verdepollo
KSH and Bash expansions are guaranteed to create an alphabetically sorted list of names before "feeding" it to cat.

Besides, file name expansion is performed before any redirection occurs.

Not sure about other shells.
Hi verdepollo,
You're correct. A POSIX conforming shell will perform pathname expansions before performing redirections.

Nonetheless, I have seen enough scripts fail because an output file was matched by an input pathname pattern expansion that I advise script writers to avoid the issue. When testing a new script and when a script fails for some reason and needs to be restarted, users frequently forget that they need to remove the output file before restarting the script. Even when it works, a user may want to run the script again the next day, week, or month and the output file can then accidentally become both an input file and an output file, leading to unexpected results. And if it is running on a system being shared with other users, the mistake can affect everyone using that filesystem; not just the user that forgot to remove the output file before restarting the script. Furthermore, when the script completes successfully, users often want to remove the (no longer needed) input files. If the output file matches the pattern that was used to select the input files, it is too easy to accidentally remove not only the input files, but also the output file.

Therefore, I strongly urge programmers not to create an output pathname that will be matched by the expansion of an input file pattern even though the output file should not be present when the script starts.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-10-2013
True, but we cannot blame the shell or a tool like cat for such behavior.

Personally, I wouldn't call it "safer use of cat" but poor script validation.

Quote:
Will this also work with characters such as ä ü ä ß etc. ?
Yes.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash selective copy folders and content to another location

I'm looking for a bash scrypt to copy some folders and some of the content to another location. I'm a teacher and very noobish with programming language anyway what I'm looking for , I have this director structure Main director "Students" with subfolders "john";"daisy";"work" etc .. and some of... (2 Replies)
Discussion started by: brickleul
2 Replies

2. Shell Programming and Scripting

How to replace multiple file content?

Hi Gurus, I need replace multiple files content. the file name pattern likes currentfile_code_* the content pattern in the file like text=value I need replace the content as text=abcde Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

3. UNIX Desktop Questions & Answers

copy content of file to another

I have a file called part1.pdb...contents are: ATOM 1 N SER 1 -10.295 -8.909 10.913 1.00 0.00 ATOM 2 H1 SER 1 -10.230 -8.214 10.183 1.00 0.00 ATOM 3 H2 SER 1 -9.558 -8.745 11.584 1.00 0.00 ATOM 4 H3 SER 1 -11.255 ... (1 Reply)
Discussion started by: kanikasharma
1 Replies

4. Shell Programming and Scripting

How to copy mail content in a file in Unix

Hi Guys I want to write a script which search mail with subject line and after that I want the mail content in a file please help guys. Thanks Atul Singh (3 Replies)
Discussion started by: atul9806
3 Replies

5. Shell Programming and Scripting

copy content of file to another files from certain position

Hello Guys I have a directory of xml files (almost 500 ) Now in each xml file 3 new attributes will be added which are in a different single file Is there any way I can copy the whole content of single file at 6th line of all the files in the directory . Thanks a lot!!! (7 Replies)
Discussion started by: Pratik4891
7 Replies

6. UNIX for Dummies Questions & Answers

Deleting Multiple Files With the Same Content

My website has been hacked and i need to remove a lot of unique files with same content. The files have very similar code: eval(base64_decodeHow do i find multiple/all the files with the above code and remove them? Thanks. (10 Replies)
Discussion started by: Boris_yo
10 Replies

7. Shell Programming and Scripting

Get multiple line content between two chars

Hello - I have a file that has the something like the following : REM CREATE TABLE lots of text REM table specifc creation text ; REM ALTER TABLE lots of text REM text specific to the the alter command REM could be more lines of text; What I need is to get all the lines for the ALTER... (2 Replies)
Discussion started by: Feliz
2 Replies
Login or Register to Ask a Question