简体   繁体   中英

local:not in a function for variable

I keep getting "12: local: not in a function" for the code below, which is the first line where "local log" appears. The local variables appear to be set properly in the function. Could someone please point me in the right direction?

#!/bin/bash

HOST="0.0.0.0"
USER="root"
PASS="123"

function login{
 if [ -z "$USER" ]
 then
   echo "pass not set"
 else 
   local log="-u $USER"
   local log+="-p$PASS"
   echo $log
fi 
}

mysql (login) -h $HOST

This is on the Ubuntu subsystem on Win 10 in case that matters.

function login {
              ^

A space is required before the curly brace.

mysql $(login) -h $HOST
      ^

You're also missing a dollar sign here.

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