Shell variable problems

Source: Internet
Author: User
Shell variable problems-general Linux technology-Linux technology and application information, the following is a detailed description. [I = s] This post was last edited by theegg at, January 11 ,.

I have two shell files
1. main. sh
_ Active = 10; # value assignment
. Function. sh # Call the function File
Change_var # execution function

2. function. sh
Change_var (){
_ Active = 200;
}

I run main. sh

Why is the value of _ active not changed?
========================================================== ===
Thank you for your answers.
I will describe the problem in detail, because I was just learning shell and may not be able to describe it well.

1. I want to make a command to integrate all common commands, such as./ben. sh db-a add-d 'dbname ',
2. File structure
~ /Ben. sh
~ /Include/function/init. sh
~ /Include/function/db. sh
~ /Logs/
3. File Code

Ben. sh
-----------------------
#! /Bin/bash

###########################
# Ubuntu 10.10

###########################
# Init variable
Declare-a _ ITEMS = ('db' 'user' 'install ');

# Init db
Declare-a _ db_act = ('login' 'import' 'dump' 'drop' 'add' 'user ');
_ Db_active = 'login ';
Export _ db_active;
Declare-x _ db_user = 'root ';
Declare-x _ db_pass = '20140901 ';
Declare-x _ db_database = '';
Declare-x _ db_new_user = '';

# Init user

###########################
# Init function
. $ PWD/include/function/init. sh;

###########################
# Check User Permissions

_ USER_NAME = $ (whoami );
If [$ {_ USER_NAME} = 'root']; then
_ C_msg "----> ";
Else
_ C_msg "Current Login User $ {_ USER_NAME}, switch to root user ";
Exit 1;
Fi

#########################
# Parameters
For item in $ {_ ITEMS}
Do
If [$ item = $1]
Then
_ C_include "$ item ";
_ C_getopts "$ item ";
Fi
Done


Include/function/init. sh
----------------------------------
#! /Bin/bash

###########################
# Some common functions
# Output information
_ C_msg (){
Echo '-->' $1;
}

# Output logs
_ C_log (){
Echo "sdf ";
}

# Targeting parameters
_ C_getopts (){
_ $ Response getopts
}

# Load Functions
_ C_include (){
For file in $ (ls $ PWD/include/function/$1 /)
Do
If [-x $ file]
Then
Chmod 777 $ PWD/include/function/$1/$ file;
Fi
. $ PWD/include/function/$1/$ file;
Done;
}



~ /Include/function/db. sh
-------------------------------
#! /Bin/bash

# Parameter allocation
_ Db_getopts (){
Echo & quot; 123 & quot ";
While getopts a: u: p: d: nu: db_arg
Do
Case "$ db_arg" in
A) _ db_active =$ {OPTARG}; export _ db_active ;;
U) _ db_user =$ {OPTARG };;
P) _ db_pass =$ {OPTARG };;
D) _ db_database =$ {OPTARG };;
Nu) _ db_new_user =$ {OPTARG };;
Esac
Done
Echo $ _ db_active;
'_ Db _' "$ _ db_active ";
}

#############################
# Function
_ Db_help (){
Echo "-";
}

_ Db_install (){
Echo "-";
}

_ Db_login (){
Mysql-u $ {_ db_user}-p $ {_ db_pass };
}

_ Db_import (){
Echo "-";
}

_ Db_dump (){
Echo "-";
}

_ Db_drop (){
Echo "-";
}

_ Db_add (){
Mysql-u $ {_ db_user}-p $ {_ db_pass}-e "create database $ {_ db_datebase }"
}

_ Db_user (){
Echo "-";
}


4. Problem
When I run the./ben. sh db-a add-d test, _ db_active variable, the value cannot be obtained through the parameter. The same is true for other parameters. This variable is defined in the starting script. Can I assign a value in a subscript?

I beg you to give me some advice,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.