0

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?

5
  • or rather /folder1/scriptName < fileName(input) Commented Sep 28, 2014 at 4:09
  • yes, you can do that. Use ./path/to/script < file_name. Commented Sep 28, 2014 at 5:42
  • thanks, do you know if you can use a directory instead of a file name as the parameter? Commented Sep 28, 2014 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 Commented Sep 28, 2014 at 5:55
  • there is a similar question asked here: unix.stackexchange.com/questions/36621/…. Read this for info: mywiki.wooledge.org/BashFAQ/001 Commented Sep 28, 2014 at 6:17

2 Answers 2

0

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 
0

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.