Shell script to execute nohup against an inputed f

2019-06-01 07:21发布

I am constantly running commands like

nohup psql -d db -f foo1.sql >& foo1.out &
nohup psql -d db -f foo2.sql >& foo2.out &

I was wondering how to create a shellscript that takes as input the filename parameter like foo1.sql and runs the command above.

How do I write a script called test so that the command ./test foo1.sql will execute the command

nohup psql -d db -f foo1.sql >& foo1.out &

标签: bash shell
2条回答
一纸荒年 Trace。
2楼-- · 2019-06-01 07:52

The syntax for calling the scripts would be:

./stest foo1.sql

there is a shell built-in called test, so don't call your script that. No parentheses required when passing parameters.

The script is very simple:

if (( $# < 1 ))
then
    echo "Insufficient arguments" >&2
    exit 1
fi

name=${1%%\.*}
nohup psql -d db -f "$1" >& "$name.out" &
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-01 07:59

Try this

#!/bin/bash

outputFile="$(echo $1 | cut -d\. -f 1).out"

nohup psql -d db -f "$1" >& "$outputFile" &

It's not called with ./test(foo1.sql) but ./test foo1.sql, as shown after the question was edited.

查看更多
登录 后发表回答