Bash:Example
lastmod 2024-07-25created 2015-10-2041 edits
A summary of simple usage of Bash.
Categories
- Bash
- Bash:Filename
- OpticalCharacterRecognition:Example:JpgToPdf: How to convert a list of JPEG files to PDF using OCR.
- Nm#Find symbol bash script: Finds object files (
*.o) in subdirectories and searches them for the desired symbol. - Nvidia:MemoryRestartBashExample: Example that checks memory size and restarts.
- Sbt:Example:StartupScript: bash script for starting sbt.
- Dockerfile#Docker image build script: Docker image build script that can create images for individual projects.
- Favicon:Example:AutoGenerateScript: bash script that auto-generates a favicon using convert.
Debugging
How to debug Bash is as follows.
Checking root privileges
Require root privileges:
Disallow root privileges:
Check if sudo
or
Index Rename
Checking the Linux distribution
Or check it as follows.
See the lsb_release command.
Disable beep sound
Or open the /etc/profile file and add the following line.
#!
- See Shebang.
Whereis
To find the full path of a particular file, enter the following.
For reference, the code that does the same thing in cmd is as follows.
User input
You can get user input as follows.
# USER_TEMP 변수에 사용자 입력값을 적용한다.
read USER_TEMP
# 1 글자만 입력 받는다.
read -n 1 USER_TEMP
# 미리 문자열을 출력한다.
read -p "Repository username: " USER_TEMP
# 보안속성을 추가한다. (사용자 입력이 출력되지 않는다)
read -s -p "Repository password: " USER_TEMP
For reference, you can confirm YES/NO as follows.
read -p "Do you wish to install this program?" yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
Using a variable name as an argument (${!param})
then execute:
Command-line user input
Sometimes you want to pass user input on the command line, for example from a script.
First, given a file like the following:
# test.sh
$ read value
$ echo '1: ' $value
$ read value
$ echo '2: ' $value
$ read value
$ echo '3: ' $value
use it as follows.
How to iterate over arguments
The eval builtin command
To enter a Bash command directly, use the eval command.
Input LOOP via PIP
You can provide an input loop via PIP as follows.
Checking the EXIT CODE
The last EXIT CODE can be checked as follows.
Code depending on existence (Exist)
To check if a directory exists in a bash shell script you can use the following:
if [ -d "$DIRECTORY" ]; then
# Will enter here if $DIRECTORY exists
fi
# Or to check if a directory doesn't exist:
if [ ! -d "$DIRECTORY" ]; then
# Will enter here if $DIRECTORY doesn't exist
fi
# In Cshell:
if ( ! -e $DIRECTORY ) then
# Will enter here if $DIRECTORY doesn't exist
endif
# check if a file doesn't exist:
if [ ! -f /tmp/foo.txt ]; then
echo "File not found!"
fi
Printing script contents (debugging)
Setting set -x for debugging prints all of the script's contents. To print whether debugging is on, check as follows.
Filling a whole line with "-"
Detects the terminal width by reading the value of the COLUMNS variable. (If that variable is not set, tput is used.)
cols=${COLUMNS:-$(tput cols)}
## "-"로 한 줄을 채웁니다.
printf -v line '%*s' "$cols" ''
echo "${line// /-}"
Expansion of variable inside single quotes
How to add an environment variable inside quotes is as follows.
User defined function
Platform checker
To identify the OS currently in use, do the following.
case "$(uname -s)" in
Darwin)
echo 'Mac OS X'
;;
Linux)
echo 'Linux'
;;
CYGWIN*|MINGW32*|MSYS*)
echo 'MS Windows'
;;
*)
echo 'other OS (or missing cases for above OSs)'
;;
esac
Function object
Function objects can be created and used as follows.
#!/bin/sh
function test1 {
echo FUNCTION_TEST1
}
function test2 {
echo FUNCTION_TEST2
}
declare -a VAL
VAL[0]=test1
VAL[1]=test2
function run {
local index=$1
${VAL[$index]}
}
run 0
run 1
Date format
The current date and time format for file names is as follows.
STDOUT and STDERR
You can add color to STDERR output as follows.
function stdout { echo "$@" }
function stderr { echo -e "\033[31m$@\033[0m" 1>&2; }
stdout 'STDOUT Message'
stderr 'STDERR Message'
Current file
Current directory
How to get the parent directory path of the invoked script file is as follows.
Note that using it as above changes the directory path, so be careful.
MD5 checksum result
MD5_CMD=`if [[ $(uname -s) == Darwin ]]; then echo 'md5 -r'; else echo 'md5sum'; fi`
CHECKSUM=`$MD5_CMD test_file_path | awk '{print($1);}'`
Upper and Lower
- Stackoverflow: Command to convert an upper-case string to lower-case?
- Stackoverflow: Converting string to lower case in Bash shell scripting
The syntax is as follows.
- lower case:
${parameter,,pattern} - UPPER CASE:
${parameter^^pattern}
$ string="Hello, World!"
$ echo $string
Hello, World!
$ echo ${string,,}
hello, world!
$ echo ${string^^}
HELLO, WORLD!
How to obtain the number of CPUs/cores
- Stackoverflow: How to discover number of logical cores on Mac OS X?
- Stackoverflow: How to obtain the number of CPUs/cores in Linux from the command line?
- How to tell how many CPUs (cores) you have on Windows 7
## Darwin:
$ sysctl -n hw.ncpu
## Linux:
grep -c ^processor /proc/cpuinfo
## Windows:
$ echo $NUMBER_OF_PROCESSORS
## OR (winbatch):
$ WMIC CPU Get NumberOfLogicalProcessors
Split string loop
sentence="one;two;three"
sentence=${sentence//;/$'\n'} # change the semicolons to white space
for word in $sentence
do
echo "$word"
done
String search
Find string
function findString {
local string=$1
local find=$2
local size=${#string}
for ((i = 0; i < $size; i++)); do
if [[ "${string:$i:1}" == "$find" ]]; then
break;
fi
done
echo $i
}
Reverse find string
function rfindString {
local string=$1
local find=$2
local size=${#string}
for ((i = $size; i >= 0; i--)); do
if [[ "${string:$i:1}" == "$find" ]]; then
break;
fi
done
echo $i
}
For reference, characters such as tab (\t) can be used as follows.
File extension
How to get a file's extension is as follows. Requires the rfindString function above.
For a simpler method, see here.
Remove file list
A script that removes files existing in a given directory (SRC_DIR) if they also exist in the target directory (DEST_DIR) is as follows.
WORKING=$PWD
SRC_DIR=train_fire_backup/
DEST_DIR=train/
for path in $SRC_DIR*; do
echo $path | sed "s;.*/;$DEST_DIR;g" | xargs rm
done
Rename file index
How to rename files in INDEX order is as follows.
WORKING=$PWD
SRC_DIR=train/
DEST_DIR=train2/
if [[ ! -d "$DEST_DIR" ]]; then
mkdir $DEST_DIR
fi
INDEX=0
for path in $SRC_DIR*.jpg; do
let "INDEX = INDEX + 1"
mv $path ${DEST_DIR}${INDEX}.jpg
done
Extract directory
Compresses every directory in the current location individually.
for i in $(find . -type d -depth 1); do printf "%s\n%s\n" "$i.tar.gz" "$i" | xargs -n 2 tar czf; done
Dynamic variable names
Multi-line string
How to assign a string value across multiple lines