Bashスクリプトは代替ファイル認識を停止します。

Bashスクリプトは代替ファイル認識を停止します。

このスクリプトがあります

#!/bin/bash
name=test.sqlite
idx=1
while [ -e $name.$idx ]; do 
    idx=`expr $idx + 1`; 
done
while [ $idx -gt 1 ]; do 
    nidx=`expr $idx - 1`; mv $name.$nidx $name.$idx; idx=$nidx; 
done
[ ! -e $name ] || mv $name $name.1
sqlite3 test.sqlite < /path/to/db_schema.sh

そしてdb_schema.sh

create table objects(id integer primary key autoincrement, name text, crc integer not null);
create index objects_idx on objects(crc);

create table symbols(id integer primary key autoincrement, name text, crc integer not null);
create index symbols_idx on symbols(crc);

create table provides(object_id integer not null, symbol_id integer not null);
create index provides_idx_sym on provides(symbol_id);
create index provides_idx_obj on provides(object_id);
create unique index provides_idx_key on provides(object_id, symbol_id);

create table depends(object_id integer not null, symbol_id integer not null);
create index depends_idx_sym on depends(symbol_id);
create index depends_idx_obj on depends(object_id);
create unique index depends_idx_key on depends(object_id,symbol_id);

これは今日でもまだ有効です。今まで試したこと

  1. ハードコーディングされたパスdb_schema.sh
  2. 変数をファイル($DB_SCHEMA_VAR)に設定
  3. 変数にパスを設定しますが、ファイルをハードコードします($PATH_TO_FILE/db_schema.sh
  4. ファイルのあるフォルダとその上の3つのフォルダを変更してください。

また、ファイルが実行可能であることを確認し、ファイルが存在するかどうかをテストするために小さな行を作成しました。

if [ -f db_schema.sh ] ; then .... 

データベースを作成する前に。テストdb_schema.shの結果、存在することがわかりましたが、一度db_schema.sh 呼び出されると、突然もう存在しません。

Barefoot IOに必要なログ出力は次のとおりです。

+ DB_INIT (this is because i call it as a function and it is not a stand-alone script)
+ cd DB_wdsfasdfg
+ name=test.sqlite
+ idx=1
+ '[' -e test.sqlite.1 ']'
+ '[' 1 -gt 1 ']'
+ '[' '!' -e test.sqlite ']'
+ sqlite3 test.sqlite
./files/functions/init_db.sh: line 22: ./files/functions/db_schema.sh: No such file or directory

ベストアンサー1

init_db.sh と db_schema.sh が同じディレクトリにあるとします。

cd現在の作業ディレクトリに関連付けられているファイル名との組み合わせは、./files/functions/db_schema.sh現在の作業ディレクトリが./files/functions/init_db.sh呼び出されたディレクトリではなくなったため、db_schema.shが見つからないことを示します。

スクリプトが以前に正しいディレクトリにあった場合は、呼び出す前にを使用してcd DB_wdsfasdfgそのディレクトリに戻るだけで十分です。cd -sqlite3

/または、現在のディレクトリとは無関係の絶対パス名(で始まるパス名)を指定できます。

おすすめ記事