在bash执行中运行source

时间:2015-06-18 06:29:30

标签: bash

快速了解我正在做的事情,我正在使用virtualenv在我的计算机上创建python开发环境。我不是一个记住所有这些命令的人,所以我喜欢构建简单的(noob-ish)脚本来帮助我。

问题

正如您在文档package com.example.reminderapp; import java.util.ArrayList; import android.app.PendingIntent; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "reminder_data"; private static final String DATABASE_TABLE = "reminder_details"; private static final String DATABASE_VERSION = "1"; public static final String REM_TITLE = "title"; public static final String REM_BODY = "details"; public static final String REM_DATE = "date"; public static final String REM_TIME = "time"; public static final String REM_MAIL = "mail"; public static final String REM_MSG = "msg"; public static final String REM_ALARM_STATUS = "alarm_status"; public static final String REM_MAIL_STATUS = "mail_status"; public static final String REM_MSG_STATUS = "msg_status"; public static final String REM_ROWID = "id"; private SQLiteDatabase remDatabase; private DatabaseHelper dbHelper; PendingIntent i; Intent intent; int requestCode; private Context remContext; public DatabaseHelper(Context context) { super(context, DATABASE_NAME , null, 1); remContext = context; } private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE +" (" + REM_ROWID + " integer primary REM autoincrement, " + REM_TITLE + " text not null, " + REM_BODY + " text not null, " + REM_DATE + " text not null, " + REM_TIME + " text not null" + REM_MAIL + " text not null" + REM_MSG + " text not null);"; @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } // // public DatabaseHelper open() throws SQLException // { // dbHelper = new DatabaseHelper(remContext); // remDatabase = dbHelper.getWritableDatabase(); // return this; // } public Cursor getData(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null ); return res; } public void close() { dbHelper.close(); } public long insertReminder(String title, String body, String reminderDate, String reminderTime, Boolean alarmStatus, Boolean mailStatus, Boolean smsStatus, String mailId, String mobileNo) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues initialValues = new ContentValues(); initialValues.put(REM_TITLE, title); initialValues.put(REM_BODY, body); initialValues.put(REM_DATE, reminderDate); initialValues.put(REM_TIME, reminderTime); initialValues.put(REM_ALARM_STATUS, alarmStatus); initialValues.put(REM_MAIL_STATUS, mailStatus); initialValues.put(REM_MSG_STATUS, smsStatus); initialValues.put(REM_MAIL, mailId); initialValues.put(REM_MSG, mobileNo); // intent.putExtra("requestCode", requestCode); Toast.makeText(remContext, "reminder created", Toast.LENGTH_SHORT).show(); return remDatabase.insert(DATABASE_TABLE, null, initialValues); } public Integer deleteReminder(Integer reminderId) { i = PendingIntent.getActivity(remContext, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); Toast.makeText(remContext, "reminder deleted", Toast.LENGTH_SHORT).show(); Log.d("database", "deleted"); return remDatabase.delete( DATABASE_TABLE,"id = ? ", new String[] { Integer.toString(reminderId) }); } public boolean updateReminder (long id, String title, String body, String reminderDate, String reminderTime, Boolean alarmStatus, Boolean mailStatus, Boolean smsStatus, String mailId, String mobileNo, int requestCode) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(REM_TITLE, title); contentValues.put(REM_BODY, body); contentValues.put(REM_DATE, reminderDate); contentValues.put(REM_TIME, reminderTime); contentValues.put(REM_ALARM_STATUS, alarmStatus); contentValues.put(REM_MAIL_STATUS, mailStatus); contentValues.put(REM_MSG_STATUS, smsStatus); contentValues.put(REM_MAIL, mailId); contentValues.put(REM_MSG, mobileNo); // i = PendingIntent.getBroadcast(remContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); db.update("reminder_details", contentValues, "id = ? ", new String[] { Long.toString(id) } ); Toast.makeText(remContext, "reminder updated", Toast.LENGTH_SHORT).show(); return true; } } 部分的step 2上看到的那样。我需要运行一个命令:

Basic Usage

这会激活我的python环境。所以我尝试了$ source venv/bin/activatefunctions的混合物。如上所述,在bash脚本方面,我有点像菜鸟。

我的代码

档案:eval

fpyenv

这应该是它的样子:

第1步

#!/bin/bash
# $ cd ~/scripts/
# $ chmod u+x <filename>.sh

activateSrc(){

    eval '$(source "${1}"/bin/activate)'

}
if [[ $1 == '' ]];
    then
    printf "ERROR: Missing Filename\n"
else
    printf "Creating Virtual Environment $1\n"
    # This creates the environment
    virtualenv $1  
    printf "Do you want to activate $1 as your virtual environment?(y/n)\n"
    # Get answer from user
    read answer 
    if [[ $answer != 'y' ]];
        # Said No 
        then 
        printf "Did not set $1 as your virtual environment\n"
    else
        # Said Yes
        activateSrc $1
        printf "Set $1 as your virtual environment\n"
    fi
fi

返回

myComputer $ fpyenv venv

步骤2(用户输入y / n)

Creating Virtual Environment venv
Do you want to activate venv as your virtual environment?(y/n)

返回

y

但我得到的是:

Set venv as your virtual environment
(venv)myComputer $ 

因此它不会运行Set venv as your virtual environment myComputer $ 。有谁知道我做错了什么?我已经查看了许多答案,但鉴于source venv/bin/activate在bash脚本中通常以不同的方式使用,我得到的答案没有帮助。提前谢谢!

FIX:

source更改为:

activateSrc

执行这样的脚本:

activateSrc(){

    source $1/bin/activate

}

1 个答案:

答案 0 :(得分:2)

它就像写的那样运行source。问题是你需要来源这个新脚本,否则它只是在子shell中运行,当子shell退出时,源脚本所做的任何更改都会丢失。