简体   繁体   中英

Execute bash commands that are included in a csv file

I have a csv file with only one column and each row includes a custom bash function, like this:

my_func 1 a
my_func 1 b
my_func 1 c

where the my_func function is included in the ~/.bash_profile , which I can run normally in the bash shell. What I want to do is to run each one of the above commands in a bash shell sequentially. Is there an easy way to do so (I want to avoid copy/paste each command in the bash shell)?

Edit: When I try to bash filename.csv I get the following error: line1: my_func: command not found .

If that's the file as you portray it then

/bin/bash <yourfile>

really doesn't matter what the file name ending is. You could also stick a:

#!/bin/bash

at the top, give run permission ( chmod u+x yourfile ) and run it as:

./yourfile

Your CSV file, is in fact, a bash script.

EDIT

Following your edit, you can source the csv in current bash shell, so you have access to the function:

. yourfile

I think the second method (adding a shebang and running the file directly) will also work - if not, try putting the function in .bashrc rather than the profile. More solutions - source the profile at the top of the CSV, define the function at the top, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM