linux bash tutorial


Guide

copy and zip

copy_and_zip.sh

folder_name=v1.2
folder_zip_name=$folder_name.zip 
folder_path=dist/$folder_name
echo $folder_path
mkdir -p $folder_path
cp -r model static templates $folder_path
cp db.py util.py demo.py README.md CHANGELOG.md $folder_path
echo "Copying to $folder_path OK"

cd dist 
zip -r $folder_zip_name $folder_name
echo "Zip to $folder_zip_name OK"

copy libs to dist

copylib.sh

#!/bin/sh
# usage:  ./copylib.sh ./example_opencv

bin=$1         
dest="./lib" 

if [ ! -d $dest ];then
    echo "makedir $dest"
    mkdir $dest
fi 

libs=$(ldd $bin | awk  '{if (match($3,"/")){ printf("%s "),$3 } }')
#echo $libs
cp $libs $dest
echo "Done"

run scripts to copy libs to lib folder.

./copylib.sh ./example_opencv

tree lib
lib
├── libc.so.6
├── libdl.so.2
├── libgcc_s.so.1
├── libjasper.so.1
├── libjbig.so.0
├── libjpeg.so.8
├── liblzma.so.5
├── libm.so.6
├── libopencv_core.so.3.1
├── libopencv_imgcodecs.so.3.1
├── libopencv_imgproc.so.3.1
├── libpng12.so.0
├── libpthread.so.0
├── librt.so.1
├── libstdc++.so.6
├── libtiff.so.5
└── libz.so.1

0 directories, 17 files

linuxdeployqt Makes Linux applications self-contained by copying in the libraries and plugins that the application uses, and optionally generates an AppImage. Can be used for Qt and other applications.

array demo

#!/bin/bash

content=$(sed -n '4p' 1.csv)
echo $content

row=`wc -l < 2.csv`
echo $row

i=0
for id in  `cat 2.csv`
do
    if [ $id == $content ]
    then
        echo $id
    else
        echo 'not equal'
        ((i=i+1))
    fi
done
echo $i

# array
echo '---------------------------------------'
arr=(Hello World)
echo ${arr[0]}
echo ${arr[1]}

arr[0]="xxx"
arr[1]="yyy"
echo ${arr[0]}
echo ${arr[1]}

echo '---------------------------------------'

# array2
array=(one two three four [5]=five)

echo "Array size: ${#array[*]}"

echo "Array items:"
for item in ${array[*]}
do
    printf "   %s\n" $item
done

echo "Array indexes:"
for index in ${!array[*]}
do
    printf "   %d\n" $index
done

echo "Array items and indexes:"
for index in ${!array[*]}
do
    printf "%4d: %s\n" $index ${array[$index]}
done

echo '---------------------------------------'

# array3
array=("first item" "second item" "third" "item")

echo "Number of items in original array: ${#array[*]}"
for ix in ${!array[*]}
do
    printf "   %s\n" "${array[$ix]}"
done
echo

# "${arr[*]}" returns all the items as a single word, whereas "${arr[@]}" returns each item as a separate word.

arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
    printf "   %s\n" "${arr[$ix]}"
done
echo

arr=("${array[*]}")
echo "After * quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
    printf "   %s\n" "${arr[$ix]}"
done
echo

arr=("${array[@]}")
echo "After @ quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
    printf "   %s\n" "${arr[$ix]}"
done

Reference

History

  • 20190308: created.

Author: kezunlin
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source kezunlin !
评论
  TOC