Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Say I want to use the name of a file as input, and process it with a pre-made script in a certain directory. How do I do it?

fileName < /folder1/scriptName

is this the right format?

share|improve this question
    
or rather /folder1/scriptName < fileName(input) –  asura Sep 28 '14 at 4:09
    
yes, you can do that. Use ./path/to/script < file_name. –  user2555595 Sep 28 '14 at 5:42
    
thanks, do you know if you can use a directory instead of a file name as the parameter? –  asura Sep 28 '14 at 5:44
    
a directory is a file in linux, so i think it should work. you should use commands which work on directories. If you need any other help in this matter, comment or edit your question. Otherwise if an answer solves your problem, accept it –  user2555595 Sep 28 '14 at 5:55
    
there is a similar question asked here: unix.stackexchange.com/questions/36621/…. Read this for info: mywiki.wooledge.org/BashFAQ/001 –  user2555595 Sep 28 '14 at 6:17

2 Answers 2

You need to pass the name of the file as a parameter to the script

#!/bin/bash
# myscript.sh
FILENAME=$1

echo "This is the filename:" $FILENAME

Then this is how you will call the script

./myscript.sh thisfile.txt

This will be the output for the script

This is the filename: thisfile.txt
share|improve this answer

These are some of the tests I executed:

$ vi scriptName.sh
#!/bin/bash
cat

Then executed,

$ ./scriptName < fileName

It will print the contents of the file.

This is a very basic example and the input is used only in one place. If you are processing the input in more than one place, which is usually the case when you write a script, then you will have to use the answer given by Fazle.

Hope this helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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