0

I have abc.sh file and input source (input.xyz).

I always execute the abc.sh file with input sourece (e.g. I type sh abc.sh input.xyz) However, as the number of directory increases I cannot bother to type input file name.

How do I implant the process into the abc.sh file to execute instead of typing the command (sh abc.sh input.xyz)?

The current script is this

INPUT=$1                            

n="1"                               
numb=$(sed -n "$n"p $1)             
#echo $numb
dummy="NULL"

Thank you for the comment it's my mistake. when I run the script I type

sh abc.sh 000.xyz      

everytime when I run 000.xyz will be in different number so .xyz is only fixed.

5
  • Is the input file always input.xyz? If not, how do you determine the input file?
    – muru
    May 20, 2019 at 16:40
  • 3
    Your question is not clear. Do you want your modified script to always behave as if you had typed sh abc.sh input.xyz (or abc.sh input.xyz) with your current script (and never e.g. abc.sh input2.xyz)? Or do you want your script to process all files of a certain name pattern? Please edit your question to add clarification.
    – Bodo
    May 20, 2019 at 16:42
  • 1
    It's unclear what you are trying to achieve. Try describing what you do manually, without a script. Without really understanding what you're asking, I can guess and say that you're trying to do input redirection ( ./script.sh < inputfile ). Good luck!
    – Liczyrzepa
    May 20, 2019 at 16:43
  • Thank you for pointing out the mistake @Bodo
    – DGKang
    May 20, 2019 at 17:41
  • Thank you for pointing out the mistake @Liczyrzepa
    – DGKang
    May 20, 2019 at 17:41

1 Answer 1

1

It's possible to use bash to loop through all your possible input files (e.g. 001.xyz, 002.xyz, etc.) in a directory. It may be easier to put it into a separate BASH script to organize what you are running (e.g. batch_run.sh).

For example, you can use a for loop to go through every xyz file in the current directory and run your abc.sh script on it:

for input_file in *.xyz
do
    sh abc.sh $input_file
done

You mentioned that you may have multiple directories. You can expand the wildcard (*) to account for directories too (relative to your current position):

for input_file in */*.xyz
do
    sh abc.sh $input_file
done

If your script has to be run in the same directory as the input file, you can also do various actions (e.g. cd) within the loop, such as something like this:

# remember the original directory before the for loop
original_directory = $(pwd)

# loop through all valid directories containing xyz files
for input_file in */*.xyz
do
    # enter the directory of containing the xyz file
    cd $(dirname $input_file)

    # run the script on the xyz file
    sh abc.sh $(basename $input_file)

    # return to the original directory
    cd ${original_directory}
done

It can get more or less complex depending on what you want to do and your specifications, however I hope this will give you an idea of how BASH for loops can allow you to automatically repeat commands.

2
  • 2
    I used first method which works perfectly! Thank you!
    – DGKang
    May 20, 2019 at 18:43
  • I would like to use the third method which you mentioned at last. However, I have hundreds of directories which named in number 001 002 003... in that case how to make a script? (I literally started to learn bash from today....)
    – DGKang
    May 20, 2019 at 18:45

Not the answer you're looking for? Browse other questions tagged .