简体   繁体   中英

Bash Script to run against all .log files

I currently have a bash script called (log2csv). While in the current directory I can type in terminal:

log2csv *.log

This will run the script on every .log file in the current directory.

Alternatively I can run it against a single .log file with

log2csv test1.log

Instead of typing log2csv *.log , can I have the *.log included in the script? So I can just type log2csv in the directory and it runs. I know I can alias that, but I rather have the script do it.

Here is the bash script I am running:

#!/bin/bash

for path
do 
      base=$(basename "$path")
      noext="${base/.log}"
      [ -e "${noext}.csv" ] && continue
      /Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"

done

Change:

for path

to:

for path in *.log

or, perhaps better:

names=( "$@" )
if [ "${#names}" = 0 ]
then names=( *.log )
fi
for path in "${names[@]}"

and you can consider whether to set options such as shopt -s nullglob as well. This uses shell arrays to handle names with blanks etc in them. It uses command line arguments if any are given, and the list of files from *.log being expanded if there are no command line arguments.

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