2022年2月22日星期二

curl json introduction

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d @body.json -u userid:passwd http://localhost:8080/imart/logic/api/cookbook119839

Intro:

-X POST : POST or GET method.

-H "Content-Type: application/json"   : add header Content-Type

-H "Accept: application/json"              : add header Accept

-d @body.json                                   : post data with filename=body.json

-u userid:passwd                               : with userid:password

last param default url.

curl --help                           
Usage: curl [options...] <url>
 -d, --data <data>   HTTP POST data
 -f, --fail          Fail silently (no output at all) on HTTP errors
 -h, --help <category>  Get help for commands
 -i, --include       Include protocol response headers in the output
 -o, --output <file>  Write to file instead of stdout
 -O, --remote-name   Write output to a file named as the remote file
 -s, --silent        Silent mode    
 -T, --upload-file <file>  Transfer local FILE to destination
 -u, --user <user:password>  Server user and password
 -A, --user-agent <name>  Send User-Agent <name> to server
 -v, --verbose       Make the operation more talkative
 -V, --version       Show version number and quit
This is not the full help, this menu is stripped into categories.
Use "--help category" to get an overview of all categories.
For all options use the manual or "--help all".

 

shell for loop in ls with filenames have spaces. IFS=$(echo -en "\n\b")

 ref: https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html


#save current ifs singno
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for src_file in `find $rootpath -type f -name "* *" | grep -iE "\.($namefilter)" `; do
    echo "src_file=" $src_file 
done
#
IFS=$SAVEIFS


files=(`ls -1 zip/`)
for file_name in "${files[@]}"; do
    echo ${file_name}
   # cp -rf zip/${file_name}/src/main src/
done

gitlab github sync

1. add github remote url 
git remote add github https://github.com/repo-name/testscript.git
git remote remove github https://github.com/repo-name/testscript.git

.git/config:
[remote "github"]
url = https://github
.com/repo-name/testscript.git
fetch = +refs/heads/*:refs/remotes/origin/*

2. add gitlab remote url 
git remote add gitlab https://gitlab.com/repo-name/testscript.git

.git/config:
[remote "gitlab"]
url = https://
gitlab.com/repo-name/testscript.git
fetch = +refs/heads/*:refs/remotes/origin/*

3. fetch from github
git fetch github
From https://github.com/repo-name/testscript.git  
 * [new branch]        main          -> github/main    

4. fetch from gitlab
git fetch gitlab
From https://gitlab.com/repo-name/testscript.git  
 * [new branch]        master          -> gitlab/master    

5. push gitlab branch master to github
git push -u github master    #push local branch master to github(remote repo).

ref: https://git-scm.com/book/ja/v2/Git-%E3%81%AE%E5%9F%BA%E6%9C%AC-%E3%83%AA%E3%83%A2%E3%83%BC%E3%83%88%E3%81%A7%E3%81%AE%E4%BD%9C%E6%A5%AD

git branch --set-upstream-to=github/main mbp-main
git fetch mbp-main

-refusing to merge unrelated histories
$ git merge --allow-unrelated-histories target_branch
# コミットを1まとめにしたい場合
$ git merge --allow-unrelated-histories --squash target_branch

reset の取り消し
git reset --soft HEAD@{1}

#when rename remote branch: feature/202203->main, update local branch:
git branch -m feature/202203 main
git fetch github
# set-upstream-to github/main mbp-main
git branch -u github/main mbp-main
#github/HEAD set to main
git remote set-head github -a

2022年2月9日星期三

Linux Empty exist file

method1:   $ : >test.csv

method2:   cp /dev/null test.txt

method3: rm test.txt && touch test.txt

method4: sed -i -e 'd'  test.txt

method5: truncate test.txt --size 0

$ truncate --help Usage: truncate OPTION... FILE... Shrink or extend the size of each FILE to the specified size A FILE argument that does not exist is created. If a FILE is larger than the specified size, the extra data is lost. If a FILE is shorter, it is extended and the extended part (hole) reads as zero bytes. Mandatory arguments to long options are mandatory for short options too. -c, --no-create do not create any files -o, --io-blocks treat SIZE as number of IO blocks instead of bytes -r, --reference=RFILE base size on RFILE -s, --size=SIZE set or adjust the file size by SIZE bytes --help display this help and exit --version output version information and exit SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000). SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'truncate invocation'

method6: echo -n >test.txt  #echo -n: do not output \n at the end. 

ref:https://qiita.com/nabewata07/items/e67d008a1f10429bb3bf


Linux command touch

ref:https://www.ibm.com/docs/ja/aix/7.1?topic=t-touch-command

touch: ファイルの時間属性を変更可能なコマンド:

目的

ファイルのアクセス時刻と変更時刻を更新します。

構文

touch [  -a ] [  -c ] [  -m ] [  -f ] [  -r RefFile ] [ Time |  -t Time ] { File ... | Directory ... }

説明

touch コマンドは、Directory パラメーターで指定された各ディレクトリー内の、File パラメーターで指定された各ファイルのアクセス時刻と修正時刻を更新します。Time 変数の値を指定しなければ、touch コマンドは現在の時刻を使用します。存在しないファイルを指定した場合は、 -c フラグで指定しない限り、 touch コマンドがその名前のファイルを作成します。

touch コマンドからの戻りコードは、時刻を修正できなかった (存在しなかったファイル、および作成されなかったファイルを含む) ファイル数です。

フラグ

項目
説明
-aFile 変数で指定されたファイルのアクセス時刻を変更します。-m が指定されていない限り、修正時刻は変更しません。
-cファイルがまだ存在していない場合は、 ファイルを作成しません。 この条件に関する診断メッセージは書き出されません。
-fファイルの読み取り許可と書き込み許可に関係なく、更新を強制的に実行しようとします。
-mFile の修正時刻を更新します。-a が指定されていない限り、アクセス時刻は変更しません。
-r RefFile現在時刻の代わりに、RefFile 変数で指定されたファイルの対応する時刻を使用します。
Time新しいタイム・スタンプの日付と時刻を MMDDhhmm[YY] のフォーマットで指定します。
MM
その年の月 (01 から 12) を指定します。
DD
その月の日 (01 から 31) を指定します。
hh
その日の時刻 (00 から 23) を指定します。
mm
その時の分 (00 から 59) を指定します。
YY
その年の最後の 2 桁を指定します。YY 変数を指定しないと、デフォルト値は現在の年になります (70 から 99、または 00 から 37)。

YY の桁の値が 70 から 99 までの場合は、年号の上 2 桁は 19 であると見なされます。

YY の桁の値が 00 から 37 までの場合は、年号の上 2 桁は 20 であると見なされます。

-t Time現在時刻の代わりに指定された時刻を使用します。Time 変数は、10 進形式 [[CC]YY]MMDDhhmm[.SS] で指定されます。
CC
その年の最初の 2 桁 (19 から 21) を指定します。
YY
その年の最後の 2 桁 (00 から 99) を指定します。

YY の桁の値が 70 から 99 までの場合は、CC の値は 19 であると見なされます。

YY の桁の値が 00 から 37 の場合は、CC の値は 20 であると見なされます。

2038 年以降の年は、yyyy 形式で年を指定します。

MM
その年の月 (01 から 12) を指定します。
DD
その月の日 (01 から 31) を指定します。
hh
その日の時刻 (00 から 23) を指定します。
mm
その時の分 (00 から 59) を指定します。
SS
その分の秒 (00 から 59) を指定します。
  

2022年2月8日星期二

linux sed メモ

例:
sed -n -e /^-/p  -e /^+/p ./modified.diff | sed -n -e '/^+++/!p'  | sed -e 's/--- a//g' tee sed_modified.log
説明: 
1. modified.diff の-or+開始する行を出力
2. 上記出力結果に+++始まる行を対象外で出力
3. 上記出力結果に--- aを削して出力
4. 上記出力結果をscreenに表示しながら、sed_modified.logにも出力. 

sed option説明
-e : editの意味、置換処理に利用。
      例: sed -e 's/abc/ABC/g'.  s:正規表現で置換処理、g:すべてマッチ.
       echo xx34 | sed -e 's/xx\(.\)/-& -/g'   ##-> -xx3-4
       & : match value.
-E : editの意味、置換処理に利用, 拡張正規表現。
       -e : \([a-zA-Z0-9]+\)  の対応 -E (\w+)
        echo xx34 | sed -E 's/xx(\w+)/-&-/g' ##-> -xx3-4
-d:deleteの意味. !は逆意味.
      例: sed 1,3d #1行目~3行目を削除、4行目以降を出力
         sed  's/^java/!d'   #java始まる行を保留、それ以外の行を削除
-n -p: -nによりdefault出力しない、-pより対象行出力
         参照:https://hydrocul.github.io/wiki/commands/sed.html
# 先頭の行のみを出力
sed -n -e 1p

# 最後の行のみを出力
# \ はシェルのエスケープ
sed -n -e \$p

# 6行目から15行目を出力
sed -n -e 6,15p

# 奇数行のみを出力
sed -n -e 1~2p

# 1行目、6行目、11行目、16行目、、、を出力
sed -n -e 1~5p

# 正規表現にマッチする行を出力
sed -n -e /xxx/p
# これは以下と同じ
grep -e xxx

# 逆に正規表現にマッチしない行を出力(dコマンドでの例)
sed -e /xxx/d
# これは以下と同じ
grep -v -e xxx

# 正規表現にマッチする行を出力
# 先頭に \ を付ければ、正規表現を囲む記号はなんでもよい
sed -n -e '\%xxx%p'

# 1つ目の正規表現にマッチする行から2つ目の正規表現にマッチする行までを出力
sed -n -e /xxx/,/yyy/p

# 正規表現にマッチする行から3行分を出力
# 正規表現にマッチするごとに最低3行が出力される
sed -n -e /xxx/,+3p

# 正規表現にマッチする行から最後までを出力
# $ が最後という意味になる
sed -n -e '/xxx/,$p'

# 20行目から23行目までの4行分を出力
sed -n -e 20,+3p
    

2022年2月7日星期一

Linux shell ln

#create a soft link to ./script/toUTF8.sh  in current directory.
ln -s ./script/toUTF8.sh  toUTF8.sh 

#check link created successfully.
ls -l 
lrwxrwxrwx  1 user user    18 Feb  7 11:50 toUTF8.sh -> ./script/toUTF8.sh
---------------------------------------------------------------
$ ln --help
Usage: ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
  or:  ln [OPTION]... TARGET                  (2nd form)
  or:  ln [OPTION]... TARGET... DIRECTORY     (3rd form)
  or:  ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist.  Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.
Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]      make a backup of each existing destination file
  -b                          like --backup but does not accept an argument
  -d, -F, --directory         allow the superuser to attempt to hard link
                                directories (note: will probably fail due to
                                system restrictions, even for the superuser)
  -f, --force                 remove existing destination files
  -i, --interactive           prompt whether to remove destinations
  -L, --logical               dereference TARGETs that are symbolic links
  -n, --no-dereference        treat LINK_NAME as a normal file if
                                it is a symbolic link to a directory
  -P, --physical              make hard links directly to symbolic links
  -r, --relative              create symbolic links relative to link location
  -s, --symbolic              make symbolic links instead of hard links
  -S, --suffix=SUFFIX         override the usual backup suffix
  -t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create
                                the links
  -T, --no-target-directory   treat LINK_NAME as a normal file always
  -v, --verbose               print name of each linked file
      --help     display this help and exit
      --version  output version information and exit
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:
  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups
Using -s ignores -L and -P.  Otherwise, the last option specified controls
behavior when a TARGET is a symbolic link, defaulting to -P.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/ln>

Oracle Session Locked Check and unlock

-- SELECT LOCKED INFO'
SELECT SID
      ,SERIAL#
      ,SADDR
      ,MACHINE
      ,USERNAME
      ,PROGRAM 
FROM   V$SESSION 
WHERE  SID = ANY(SELECT SID 
                 FROM V$LOCK 
                 WHERE TYPE IN ('TX','TM') 

                 );  

-- UNLOCK SESSION

ALTER SYSTEM KILL SESSION 'SID, SERIAL#';