Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to create a file or folder and the same time write it to the .sh file?

For example: word count for a file, write that info to a new file and save it in the .sh file at the same time.

I tried:

echo 'wc filename.txt > ./folder/newfilename.txt' >>ToThefilename.sh

it would give me the word count for file name in the folder, but in the sh file it would give me what's on the quotes.

share|improve this question

closed as unclear what you're asking by Radu Rădeanu, guntbert, BuZZ-dEE, Mitch, Anwar Shah Feb 24 at 12:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Can you provide a more complete example? What should "ToTheFilename.sh" look like? –  Amanda Feb 23 at 14:56
    
the results of the commands instead of what I typed in quotes –  user251427 Feb 23 at 15:03
    
Right. So please update your question to include a code block that shows us an example of that. –  Amanda Feb 23 at 15:09
    
@user251427 Did you mean to write output of wc filename.txt run from a .sh file to another text file at ./folder/newfilename.txt? –  souravc Feb 23 at 15:17
    
Can you clarify your question? What exactly do you want to write in the ToThefilename.sh file? If you mean the output of wc filename.txt > ./folder/newfilename.txt this is always null if the command ends with success. So, nothing to write in ToThefilename.sh file... –  Radu Rădeanu Feb 23 at 15:50
add comment

2 Answers 2

Echo will print whatever you've given it in quotes, literally. So

echo 'wc filename.txt > ./folder/newfilename.txt' >>ToThefilename.sh

Should append this text to the end of ToThefilename.sh

wc filename.txt > ./folder/newfilename.txt

If you want to write the output of wc filaname.txt to "ToThefilename.sh" you'd just do wc filename.txt >>ToThefilename.sh So if I do wc examples.desktop > foo.txt then foo.txt will contain the results of that command:

amanda@mona:~$ cat foo.txt 
 240  569 8980 examples.desktop

if you just want to keep a log of things you've done to or with filename.txt you can do history | grep filename.txt > ToThefilename.sh

If that isn't what you're trying to accomplish, please clarify your question! To do something more complex, you're probably going to need to write a shell script that takes the input and does a few different things with it.

share|improve this answer
add comment

You can do this with tee and a process substitution:

echo 'wc filename.txt' | tee >(bash > ./folder/newfilename.txt) >> ToThefilename.sh

The process substitution acts like a file for the tee command. tee will pipe the output of the echo command to both the file and to stdout (where the redirection appends it to the script). bash will read the command on its stdin

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.