2019年3月21日 星期四

在執行期取得目前Makefile腳本檔所在的目錄 Absolute Path of current Makefile 含測試結果


範例程式取得Makefile所在絕對路徑 (Absolute Path of current Makefile)


current_mk_abspathname := $(abspath $(lastword $(MAKEFILE_LIST)))
current_mk_absdir := $(dir $(current_mk_abspathname))
all:
@echo "Makefile absolute pathname: $(current_mk_abspathname)" ; \
echo "Makefile absolute path: $(current_mk_absdir)"

test results on linux


/build$ make -v
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
/build$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
/build$ make -f 2019-03-21/01-abspath-mk.mk
Makefile absolute pathname: /build/2019-03-21/01-abspath-mk.mk
Makefile absolute path: /build/2019-03-21/


參考

https://www.systutorials.com/241620/how-to-get-the-full-path-and-directory-of-a-makefile-itself/

2019年3月14日 星期四

Bash Tip - Run Command with multiple arguments with ssh




This is a note for logging my simple way to use ssh and bash with continuous integration on remote server.

You can execute the following test-cases.sh on your host.

It will execute remote script of the test with correct argument contents.

source code of test-cases.sh on host


#/bin/bash
# 2019 Ben Wei <ben@juluos.org>
#
# This is a example script for CI tests for blog
# if not defined in environment variable
# you can use the following command to defined it
#
# export TEST_SSH_HOST="your_ssh_host"
#
function run_cases()
{
local case_name="$1"
local args=""
while [ ! "x$1" = "x" ]; do
printf -v __ %q "$1"
args="$args \"$__\""
shift
done
if [ "x${TEST_SSH_HOST}" = "x" ]; then
TEST_SSH_HOST=test-server
fi
ssh ${TEST_SSH_HOST} "cd test-cases; /bin/bash ./tests.sh $args"
return $?
}
view raw test-cases.sh hosted with ❤ by GitHub



directory layout of tester account on test-server



source code of tests.sh on test-server



#!/bin/bash
i=1
while [ ! "x$1" = "x" ]; do
echo "argv[$i]=[$1]"
shift
i=$((i + 1))
done
view raw tests.sh hosted with ❤ by GitHub

result of test-cases.sh example

source ./test-cases.sh
➜  ~ run_cases "\"1\"\"" "b'\"" "c'#\"" "\"\"" "\'"
ssh test-server cd test-cases; /bin/bash ./tests.sh  "\"1\"\"" "b\'\"" "c\'\#\"" "\"\"" "\\'"
argv[1]=["1""]
argv[2]=[b\'"]
argv[3]=[c\'\#"]
argv[4]=[""]
argv[5]=[\\']

Notes

printf %q  is use to make safe quote the argument if hashtag(#), quote('), double quote(") and so on.

Appendix