2020年7月30日星期四

github/gitlab 二段階認証の場合、commit/pushエラー解決策

github/gitlab 二段階認証の場合、commit/pushエラー解決策:
password: personal access token
例:

git init
git add *
git commit -m "first commit"
git remote add origin https://github.com/username/test

git push -u origin master
--confirm of username/usermail and password
username: your username
password: not your password but your personal access token.

how to get your password access token?
ask google :).
 

Introduction Of Git Basic Command


  before git commit:
   git config --global user.email "username@example.com" 
   git config --global user.name "username"                       


…or create a new repository on the command line

echo "# testscript" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/lqwangxg/testscript.git
git push -u origin master
                

…or push an existing repository from the command line

git remote add origin https://github.com/lqwangxg/testscript.git
git push -u origin master

…or import code from another repository

You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

…Change remote URL 

#check current URL before changing.
get remote get-url --all origin 
https://github.com/lqwangxg/beforestatus.git

#change origin URL
get remote set-url origin https://github.com/lqwangxg/test.git

#check current URL after changed.
get remote get-url origin 
https://github.com/lqwangxg/test.git

2020年7月29日星期三

shell if string is empty

1、$word is empty、true
  if [ -z ${word} ]; then 
    echo "is null"
  fi

2、$word is not empty、true
  if [ -n ${word} ]; then 
    echo "is not null"
  fi

真になる条件
文字列1 = 文字列2文字列1と文字列2が等しいとき
文字列1 != 文字列2文字列1と文字列2が等しくないとき
-z 文字列1文字列1の長さが0のとき
-n 文字列1文字列1の長さが0ではないとき
注意:=の左右に空白が必要 =

3、regex pattern check
$LANGにJPが存在するかどうか:
if [[ $LANG =~ .*JP.* ]]; then 
   echo "$LANG HAS JP EXIST."
fi


chmodにてファイルに権限設定

1、lsによりファイルの現状を確認
 ls -l   ./
-rw-rw-r-- 1 user user 16476 Jul 27 18:14 status.xlsx
-rw-rw-r-- 1 user user   432 Jul 28 10:21 test1.txt
-rwxrw-r-- 1 user user   159 Jul 29 17:07 test.sh
drwxrwxr-x 2 user user 57 Jul 29 17:08 src
説明:
-rw-r--r--について
先頭1文字の種別意味
-ファイル
dディレクトリ
lシンボリック
2~4文字目:ファイルの所有者権限
57文字目:ファイルの所有グループ権限
810文字目:その他者の権限
モード(数字)モード(アルファベット)権限
4r読み取り
2w書き込み
1x実行
2、chmodにより権限の変更設定
chmod 変更対象 変更方法 変更内容 対象ファイル
例: chmod u+x test.sh
test.shファイルに所有者の実行権限を追加する。 
変更対象意味
uユーザー
gグループ
oその他
a
すべて
変更方法意味
=指定した権限にする
+指定した権限を付与する
-指定した権限を除去する
変更内容意味
r読み取り
w書き込み
x実行
3、chmodの数字権限設定
例:chmod 764 test.sh
7 =4+2+1 所有者にr(4)w(2)x(1)権限を設定
6 =4+2+0 グループにr(4)w(2)権限を設定
4 =4+0+0 その他にr(4)のみ権限を設定

check file/directory is exists by bash shell

1、$word is empty、true
  if [ -z ${word} ]; then 
    echo "is null"
  fi

2、$word is not empty、true
  if [ -n ${word} ]; then 
    echo "is not null"
  fi

真になる条件
文字列1 = 文字列2文字列1と文字列2が等しいとき
文字列1 != 文字列2文字列1と文字列2が等しくないとき
-z 文字列1文字列1の長さが0のとき
-n 文字列1文字列1の長さが0ではないとき
3、regex pattern check
$LANGにJPが存在するかどうか:
if [[ $LANG =~ [.*JP.*] ]]; then 
   echo "$LANG HAS JP EXIST."
fi
注意: pattern should be in [pattern] or as a parameter
pattern=".*JP.*"
if [[ $LANG =~ $pattern ]]; then 
   echo "$LANG HAS JP EXIST."
fi

-------------------------------------------------
1、ファイル存在
 if [-e ${filepath}]; then 
     echo "${filepath}が存在しています"
   fi 
   #ファイル存在しない
 if [! -e ${filepath}]; then 
     echo "${filepath}が存在しません"
   fi 

2、フォルダ存在
   if [-d ${folderpath}]; then 
     echo "${folderpath}が存在しています"
   fi 
   if [! -d ${folderpath}]; then 
     echo "${folderpath}が存在しません"
   fi 
3、ファイルであるか、フォルダであるか
   if [-f ${path}]; then 
     echo "${path}はファイルです"
   else 
     echo "${path}はフォルダです"
   fi 
4、フォルダ下に特定拡張子のファイルを存在しているか
   if  ls ${path}/*.txt >/dev/null 2>&1
   then
      echo "exist"
   fi
  一行内に各場合:
 if ls ${path}/*.txt >/dev/null 2>&1; then echo "exist"; fi

 ファイルやディレクトリの存在を確認する際に使用できる演算子
演算子説明
-a ファイルファイルがあれば真
-b ファイルファイルがありブロックス特殊ファイルであれば真
-c ファイルファイルがありキャラクター特殊ファイルであれば真
-d ファイルファイルがありディレクトリであれば真
-e ファイルファイルがあれば真
-f ファイルファイルがあり通常のファイルであれば真
-g ファイルファイルがありSGID(特殊なアクセス権)であれば真
-G ファイルファイルがあり実行グループIDによる所有者であれば真
-h ファイルファイルがありシンボリックであれば真(-Lと同じ)
-k ファイルファイルがありステッキービットが設定されていれば真
-L ファイルファイルがありシンボリックであれば真(-hと同じ)
-O ファイルファイルがあり実行ユーザIDによる所有者であれば真
-p ファイルファイルがあり名前付きパイプ(named pipe)であれば真
-r ファイルファイルがあり読み取り可能であれば真
-s ファイルファイルがありサイズが0より大きければ真
-S ファイルファイルがありソケットであれば真
-t FDFD(ファイルディスクリプタ)が端末でオープンされていれば真
-u ファイルファイルがありSUID(特殊なアクセス権)であれば真
-w ファイルファイルがあり書き込み可能であれば真
-x ファイルファイルがあり実行可能であれば真
reference:
https://shellscript.sunone.me/if_and_test.html

bash Shell の基本ルール

① shellのコマンドは基本的に、小文字で書きます。
② `cmd`よりcmdを実行します。
関数()にパラメータ名がなくても、$iによりパラメータ参照が可能です。
1、shファイルの宣言
#!/bin/bash

2、変数
#コメント、文字列宣言&初期化
s1="hello"

#変数使用
echo $s1     :hello
echo "$s1"   :hello
echo "${s1}" :hello
echo $s1$s1  :hellohello
echo '$s1'   :$s1  ''の場合、変数値ではなく、そのまま
echo "$s1abc$s1"   :helloabchello
echo "$s1 $s1"   :hello hello
#数値変数宣言&初期化
s2=10

#変数使用
echo $s1     :10
echo $s1+2   :10+2  計算ではなく、。文字列とされる
echo `expr $s1+2`   :12 計算にはexpr計算式で
echo `expr $s1-2`   :8 計算にはexpr計算式で
#読み取り専用変数
readonly y=10
3、配列
a=(2 4 6)
echo $a       :2 0番と見なされ
echo ${a[0]}  :2 0番出力。{}が必須
echo ${a[1]}  :4 1番出力
echo ${a[2]}  :6 2番出力 
echo ${a[@]}  :2 4 6 全出力
echo ${#a[@]}  :3  配列数を出力される
※注意:
echo $a[0]    :2[0] 原因:2と[0]の文字列結合とみなされる。
#要素の追加
a+=(20 30)
echo ${a[@]}: 2 4 6 20 30
#要素内容変更
a[2]=10 
4、条件
数値比較(eq=, ne!=or<>, gt>, ge>=, le<=, lt<)、$?結果
test 1 -eq 2; echo $?  :1  
test 1 -eq 1; echo $?  :0  
test 1 -ne 2; echo $?  :0  
test 1 -gt 2; echo $?  :1  
文字列比較 =, !=
ファイル日付比較
-nt newerthan, -ot olderthan
論理 -a and,  -o or, !否定
5、if文
x=80
if test $x -gt 60
then 
  echo "$x>60 is true"
fi    #endif=fi
########  test =[]   #######
if [$x -gt 60]; then 
  echo "$x>60 is true" 
elif [$x -gt 50]; then    # 同じ行に複数処理を書く時に ";"が必須
  echo "$x>50 is true" 
else
 echo "$x>50 is false"
fi
6、case文
signal="red"
case $signal in 
  "red")
     echo "stop!"
     ;;           #;; により、break
   "yellow")
     echo "caution!"
    ;;
  "green")
     echo "go!"
    ;;
   *)         #default:
     echo "..."
    ;;
esac  #caseの逆
7、while文
i=0
while [ $i -lt 10]
do 
  i = `expr $i+1`
  echo ${i}
done
#無限ループ
while :
do
  j = `expr $j+1`
 if [$j -gt 10]; then
    continue
  fi
  if [$j -gt 20]; then 
    break
  fi
  echo ${j}
done
8、for文
for i in 1 2 3 4 5 
do 
   echo ${i}
done 
a=(1 2 3 4 5 7 9 10)
for i in ${a[@]}
do 
  echo ${i}
done 
for i in `seq 1 5`  # 1~5
do 
  echo ${i}  # continue, break OK.
done 
9、引数
echo $0 自身
echo $1 第一引数
echo $2 第二引数
echo ${10} 第10引数 10番目以降の引数には{}が必要
echo ${@} 引数全部
echo ${#} 引数の数
※stdin入力を受ける引数
while :
do 
  read key
  echo "you pressed ${key}"
  if [${key} = "end"]; then 
     break
  fi
done
※選択項入力により受ける引数
echo "which language do you favorite?"
select option in JAVA JS C#
do 
  echo "I LIKE ${option} too."
  break
done 
--------------------------
1) JAVA
2) JS
3) C#  
#? 2
I LIKE JS too.
10、ファイルから入力
data.txt:
hello
world
nagoya
hello.sh:
#!/bin/bash
i=1
while read line #受け取ったデータを1行ずつ処理
do 
  echo "${i}: ${line}"
  i=`expr $i+1`
done <$1     #第一引数により、ファイル名data.txtを受け渡す
実行結果:
$ ./hello.sh data.txt
1: hello
2: world
3: nagoya
11、関数
hello.sh
#!/bin/bash
function test(){
  echo "hello"
}
#functionを省略可能. 
test2(){
  echo "it's test2."
  i=5
  echo ${i}
  local j=10
  echo "${i} ${j}"
}
test2
echo "-------"
echo ${i}
echo ${j}
実行結果:
$ ./hello.sh
it's test2.
5 10
-------
5     #jは local変数のため、関数外の参照はできない、出力結果はなし

winscp コマンドでファイルの転送

windows <--> Linux間のファイル転送には、Winscpをよく利用されています。

winscpディフォルトのインストールパス:
"C:\Program Files (x86)\WinSCP\WinSCP.exe"

このパス「C:\Program Files (x86)\WinSCP\」を環境変数に設定すれば、
CMDにより、どこでも直接winscpを起動できます。

SET PATH=%PATH%;C:\Program Files (x86)\WinSCP\

CMDを起動して、WINSCPを入力して、ENTERにより、WINSCPを起動します。
C:\Users\test>winscp
 winscp>
 winscp>help
call     任意のリモート コマンドを実行
cd       リモート ディレクトリの変更
checksum リモート ファイルのチェックサムを計算する
chmod    リモート ファイルの権限を変更
close    セッションを閉じる
cp       リモート ファイルを複製
echo     その引数をメッセージとして表示します
exit     すべてのセッションを閉じプログラムを終了する
get      リモートからローカルにファイルをダウンロード
help     ヘルプを表示
keepuptodate ローカル ディレクトリの変更をリモートに反映
lcd      ローカル ディレクトリを変更
lls      ローカル ディレクトリの内容を表示
ln       リモートにシンボリックリンクを作成
lpwd     ローカル ディレクトリの表示
ls       リモート ディレクトリの内容を表示
mkdir    リモート ディレクトリを作成
mv       リモート ファイルを削除または名前変更
open     サーバに接続
option   スクリプトオプションの値を設定/表示
put      ローカルからリモートにファイルをアップロード
pwd      リモート ディレクトリの表示
rm       リモート ファイルを削除
rmdir    リモート ディレクトリを削除
session  接続しているセッションの表示またはアクティブなセッションの選択
stat     リモート ファイルの属性を表示
synchronize リモートとローカルのディレクトリを同期

初回リモートサーバへ接続には、パスワードが必要です。その後、SESSIONが出来上がり、再接続する場合には、パスワードが不要になります。
例:
winscp>open user:password@hostname/IP 
サーバを探索中・・・
サーバに接続しています・・・
認証しています・・・
ユーザ名"eru" を使用中
入力済みパスワードで認証中
認証されました
セッションを開始しています・・・
セッションを開始しました
アクティブ セッション: [1] user@hostname/IP
winscp>ls
drwxr-xr-x  14 user    user           4096 Jul 27 18:17:18 2020 .
drwxr-xr-x   3 root     root            17 Jul  9  3:37:38 2019 ..

よく利用されるコマンドは:
リモートコマンド: cd, ls, mkdir, mv, rmdir, rm, pwd,
ローカルコマンド:lcd, lls,lpwd
ファイル転送: get, put

winscp> help get
別名alias: recv, mget
補足パラメータswitches:
 -delete          Delete source remote file(s) after transfer
       転送完了後、リモートファイルを削除する
  -resume          Resume transfer if possible (SFTP and FTP protocols only)
        SFTPとFTPモードの場合、転送再開可能
  -append          Append file to end of target file (SFTP protocol only)
        SFTPモードの場合なら、目標ファイルの末尾から追加転送が可能
  -preservetime    Preserve timestamp
        伝送元先ファイルのタイムスタンプを一致させる
  -nopreservetime  Do not preserve timestamp
        伝送元先ファイルのタイムスタンプを一致しない         
  -speed=<kbps>    Limit transfer speed (in KB/s)
         伝送速度制限
  -transfer=<mode> Transfer mode: binary, ascii, automatic
        伝送モード:バイナリ、テキスト、拡張子により自動判定
  -filemask=<mask> Sets file mask.
         
  -resumesupport=<state> Configures resume support.
                   Possible values are 'on', 'off' or threshold
       転送再開可否設定。ON可能、OFF負荷、thredhold設定制限値
  -neweronly       Transfer new and updated files only
        新規または更新されたファイルのみ転送
  -latest          Transfer the latest file only
        タイムスタンプを比較し、サーバより最新ファイルのみ転送
effective options:
  confirm, failonnomatch, reconnecttime
examples:
  get index.html
  get -delete index.html about.html .\
  get index.html about.html d:\www\
  get public_html/index.html d:\www\about.*
  get *.html *.png d:\www\*.bak

putコマンドはget とほぼ一緒です。
winscp> help put
alias:
  send, mput
switches:
  -delete             Delete source local file(s) after transfer
  -resume             Resume transfer if possible (SFTP and FTP protocols only)
  -append             Append file to end of target file (SFTP protocol only)
  -preservetime       Preserve timestamp
  -nopreservetime     Do not preserve timestamp
  -permissions=<mode> Set permissions
  -nopermissions      Keep default permissions
  -speed=<kbps>       Limit transfer speed (in KB/s)
  -transfer=<mode>    Transfer mode: binary, ascii, automatic
  -filemask=<mask>    Sets file mask.
  -resumesupport=<state> Configures resume support.
                      Possible values are 'on', 'off' or threshold
  -neweronly          Transfer new and updated files only
  -latest             Transfer the latest file only
effective options:
  confirm, failonnomatch, reconnecttime
examples:
  put index.html
  put -delete index.html about.html ./
  put -permissions=644 index.html about.html /home/martin/public_html/
  put d:\www\index.html about.*
  put *.html *.png /home/martin/backup/*.bak

SESSIONを閉じる場合、
winscp> close
セッション 'user@hostname' を閉じました.
セッションなし

SESSIONを再開する場合、
winscp> open user@hostname
サーバを探索中・・・
サーバに接続しています・・・
認証しています・・・
ユーザ名"user" を使用中
入力済みパスワードで認証中
認証されました
セッションを開始しています・・・
セッションを開始しました
アクティブ セッション: [1] user@hostname

winscpを閉じる場合
winscp> exit

C:\Users\user>
=====================================
おまけに、scpのコマンドで簡単なファイル転送なら
scp [オプション] コピー元パス 保存先パス
例:
# get file from remote serverscp user@hostnameOrIP: ~/test.txt ./
# put file to pwd of remote server   
scp user@hostnameOrIP: ./test.txt