Bash Shell

External References

define LANG

  • export LANG="en_US.UTF-8"

Loop

Read file line by line

Python code shell에서 실행하기

  • argument도 pass가능함. e.g.) ${var} $PWD
  • ref: http://unix.stackexchange.com/questions/74244/hybrid-code-in-shell-scripts-sharing-variables
    csvFile_arg=$(
    python - ${var} --pwd=$CPWD <<EOF
    import optparse, sys
    p=optparse.OptionParser()
    p.add_option('--csvfile',dest='cf',action='store')
    p.add_option('--pwd',dest='pwd',action='store')
    (o,_) = p.parse_args()
    if o.cf and (not o.cf.strip().startswith('/') or o.cf.strip().startswith('.')):
        print '--csvfile=%s/%s' % (o.pwd, o.cf.strip())
    else:
        print '--csvfile=%s' % (o.cf.strip())
    EOF
    )
    

shell array에 key없이 값 추가하기

people=()
people+=('mike')
people+=('nikola')

# access entire array
${people[@]}

bash shell arguments

첫번째가 아닌 두번째 arguments부터 전달하기

Variables

Declare variable type

Wait until background subprocess are finished. (sample code)

#!/bin/sh

cat > test1.sh <<FEOF 
#!/bin/bash

for x in 1 2 3 4 5; do
    echo "tom \${x}"
    sleep 5
done
FEOF

cat > test2.sh <<FEOF 
#!/bin/bash

for x in 1 2 3 4 5; do
    echo -e "sam \${x}"
    sleep 3
done
FEOF

cat > test3.sh <<FEOF 
#!/bin/bash

for x in 1 2 3 4 5; do
    echo "wei \${x}"
    sleep 2
done
FEOF

sh test1.sh &
sh test2.sh &
sh test3.sh &

echo "start waiting all BG process."
wait

#rm test1.sh test2.sh test3.sh
echo "finished."

call functions with string values from CLI arguments.

#!/bin/sh

cat > test1.sh <<FEOF 
#!/bin/bash

for x in 1 2 3 4 5; do
    echo "tom \${x}"
    sleep 5
done
FEOF

cat > test2.sh <<FEOF 
#!/bin/bash

for x in 1 2 3 4 5; do
    echo -e "sam \${x}"
    sleep 3
done
FEOF

cat > test3.sh <<FEOF 
#!/bin/bash

for x in 1 2 3 4 5; do
    echo "wei \${x}"
    sleep 2
done
FEOF

chmod 777 test1.sh test2.sh test3.sh

JOBS=''

function job1() {
    sh test1.sh &
}
function job2() {
    sh test2.sh &
}

function job3() {
    sh test3.sh &
}

function process() {

    for j in ${JOBS}; do
        ${j}
    done

    echo "================= start waiting all BG process. =============="
    wait
    #rm test1.sh test2.sh test3.sh
    echo "finished."
}

while getopts j: o; do
    case "$o" in
        j) JOBS="$OPTARG";;
        *) echo "-j    jobs to run"
           exit 1;;
    esac
done

process

String

빈칸으로 나누어져 있는 string을 comma로 붙이기

JOBS="hello world"
_JOBS_WITH_COMMA=${JOBS// /, }
echo ${_JOBS_WITH_COMMA}
hello, world
Last modified 3 years ago Last modified on 01/25/17 17:06:08