Skip to content

Shell script

jaeseok.an edited this page Jun 11, 2019 · 8 revisions

Expression

  • increment(++) 하기
running=$((running + 1))

Function

인자 : parameters

function my_function(){
   local value=$1
   echo "my_function= ${value}"
}
 
# call my_function
my_function 5678
결과 전달받기

#!/bin/sh
function my_function(){
   local value=$1
   echo "my_function= ${value}"
}
 
# call my_function
result=$(my_function 5678)
 
echo "my_function [ ${result} ]"

function my_function(){
    echo "argv: $1 $2"
    return 10
}
 
# call my_function
my_function "myname" "acepro"
result=$?
 
echo "my_function [ ${result} ]"

function my_function(){
    echo "argv: $1 $2"
    eval "$3='result'"
}
 
# call my_function
result=""
my_function "myname" "acepro" result
 
echo "my_function [ ${result} ]"

bash expression

  • bash associative array(map)
$ declare -A MYMAP     # Create an associative array
$ MYMAP[foo]=bar       # Put a value into an associative array
$ echo ${MYMAP[foo]}   # Get a value out of an associative array
bar
$ echo MYMAP[foo]      # WRONG
MYMAP[foo]
$ echo $MYMAP[foo]     # WRONG
[foo]

simple regular expression with variable

not regular expression pattern
${variable//pattern/replacement} 
regular expression
if [[ $bpname =~ "BP*" ]]; then

substitute variable

뒤에서 lonngest match = %%, 짧은 match =%, 앞에서 match=#으로 변경
bpname=${file%%.toml}

if condition

if [ ! -e genesis -o ! -e genesis.json ];

util

  • json parser
    • jq
      • 특정 필드는 jq .필드명 으로 출력가능하다

test

Clone this wiki locally