Last active
November 28, 2018 15:54
-
-
Save kujyp/2012803263c5f69cfb45f9cc858d490d to your computer and use it in GitHub Desktop.
Shell script Cheat Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "${LOCAL_USER_ID}" ]; then | |
echo LOCAL_USER_ID is empty | |
exit 1; | |
fi | |
if [ -z "${LOCAL_GROUP_ID}" ]; then | |
echo LOCAL_GROUP_ID is empty | |
exit 1; | |
fi | |
UID_DUPLICATED_USER=`awk -F: '{ if ($3 == LOCAL_USER_ID) print $1 }' LOCAL_USER_ID="${LOCAL_USER_ID}" /etc/passwd` | |
if [ -n "${UID_DUPLICATED_USER}" ]; then | |
echo Deleting uid duplicated user, ${UID_DUPLICATED_USER} | |
userdel ${UID_DUPLICATED_USER} | |
fi | |
echo Add ftp user, as ${LOCAL_USER_ID}:${LOCAL_GROUP_ID}\(uid:gid\) | |
groupadd -g ${LOCAL_GROUP_ID} -o ftp | |
useradd -o -u ${LOCAL_USER_ID} -g ${LOCAL_GROUP_ID} ftp | |
exec "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
FTP_USER=myuser | |
FTP_PASS=mypass | |
FTP_PORT=8021 | |
# Do not use Alphabetic domain name as FTP_ADDRESS. | |
# Use numeric ip address. | |
FTP_ADDRESS="FIXME" | |
if [ "${FTP_ADDRESS}" = "FIXME" ]; then | |
echo "Fix FTP_ADDRESS first." | |
exit 1; | |
fi | |
echo -e "Example usages\n\ | |
- ./start.sh /Users/jaeyoung/workspace/docker-vsftpd\n\ | |
" | |
if [ -z "${1}" ]; then | |
echo "PATH Variable is empty" | |
exit 1; | |
fi | |
FTP_PATH=$1 | |
docker run -d -v "${FTP_PATH}":/home/vsftpd/${FTP_USER} \ | |
-p 8020:20 -p ${FTP_PORT}:21 -p 21100-21110:21100-21110 \ | |
-e LOCAL_USER_ID=`id -u` -e LOCAL_GROUP_ID=`id -g` \ | |
-e FTP_USER=${FTP_USER} -e FTP_PASS=${FTP_PASS} \ | |
-e PASV_ADDRESS=${FTP_ADDRESS} -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110 \ | |
--name vsftpd --restart=always vsftpd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Tested Environments : Centos7 | |
get_script_path() { | |
SCRIPT=`readlink -e $0` | |
echo `dirname $SCRIPT` | |
} | |
cd_into_script_path() { | |
SCRIPT_PATH=`get_script_path` | |
cd ${SCRIPT_PATH} | |
} | |
cd_into_script_path | |
source ../configs/config.sh | |
rm -rf ${COPY_PATH} | |
cp -r ${PROJ_PATH} ${COPY_PATH} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Tested Environments : Centos6 | |
start_script_on_background() { | |
echo "nohup ${1} > ${2} &" | |
nohup ${1} > ${2} & | |
} | |
get_script_path() { | |
SCRIPT=`readlink -e $0` | |
echo `dirname $SCRIPT` | |
} | |
SCRIPT_PATH=`get_script_path` | |
echo "SCRIPT_PATH: ${SCRIPT_PATH}" | |
LOGDIR="${SCRIPT_PATH}/../static" | |
PROCESS_NAME=express | |
CMD="yarn start" | |
echo "[DESC]: cd ${SCRIPT_PATH}/.." | |
cd "${SCRIPT_PATH}/.." | |
start_script_on_background "${CMD}" "${LOGDIR}/nohup_${PROCESS_NAME}.txt" |
- Variable not null check
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-docker_vsftpd_entrypoint-sh-L14-L17
- Using variable in awk statement
- https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script
echo "input data" | awk '{print var}' var="$variable"
or
awk '{print var}' var="$variable" file
- shell file exists
https://www.cyberciti.biz/faq/unix-linux-test-existence-of-file-in-bash/
file="/etc/hosts"
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
fi
if [ ! -f "/data/.htpasswd" ]; then
echo notfound
else
echo found
fi
- shell string first charactor
https://stackoverflow.com/questions/1405611/how-to-extract-the-first-two-characters-of-a-string-in-shell-scripting
long="abcdefg"
echo "${long}" | cut -c1-1
- shell insert line
https://stackoverflow.com/questions/6537490/insert-a-line-at-specific-line-number-with-sed-or-awk
sed -i '8i This is Line 8' FILE
- shell delete line
grep -v "pattern" file > temp && mv temp file
- execute shell script via curl
searching query: curl shell script execute, execute shell script via curl, shell curl
https://gist.github.com/jnbnyc/8bb4742ada04f1615267450d521e5025
curl -s URL | bash
- console color
searching query: shell color, echo color
https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"
echo -e "${RED}[ASSERTION] blarblar.${NC}"
- append file as root
searching query: sudo append
https://superuser.com/questions/136646/how-to-append-to-a-file-as-sudo
echo "output" | sudo tee -a file
or
sudo bash -c "somecommand >> somefile"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use example description
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-docker_vsftpd_start-sh-L15-L17
Variables null check
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-docker_vsftpd_start-sh-L19-L22
Get executed script path(Remove executed path dependency)
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-mtenginedependencies_start-sh-L9-L12
Wrap "Get executed script path"
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-seqtoseq_compile_and_test_cp-sh-L5-L13
Execute on background
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-mtenginedependencies_start-sh-L4-L7
String check
https://gist.github.com/kujyp/2012803263c5f69cfb45f9cc858d490d#file-docker_vsftpd_start-sh-L10