shbash控制序列(代码片段)

author author     2022-12-20     117

关键词:

#!/usr/bin/env bash

# IFs (eq, gt, ...)
FLAVOR_LANG=de
if [ "$1"=="en" ]; then
	FLAVOR_LANG=en
fi

if [ -d "$1" ]; then
  echo "Is Directory!"
fi

if [ ! -f "$1" ]; then
  echo "Is not File!"
fi

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# check if variable is set
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
if [ -z $var+x ];
if [ -z "$var" ]; # does not distinguish betw. unset and empty!

if (( number comparison )); then ...
# IF operators
! # not
-n # len of str > 0
-z # len of str = 0
=, != # str = str
-eq,-gt,-lt # num =,>,< num
-d # exist and dir
-f # exist and file
-e # exist

IF [] # [] is short for `test` command
IF [] || []; then
  # a
ELIF [] && []; then
  # b
ELSE
  # c
FI

# Lists
ARRAY=(
	file1.pdf
	file2.pdf
)

# Loops (break, continue supported)
for current in $ARRAY[@]; do
  echo $current
done

ELEMENTS=$#ARRAY[@]
for (( i=0;i<$ELEMENTS;i++)); do
    echo $ARRAY[$i]
done

for value in 10..0..2
do
  echo $value
done

counter=1
while [ $counter -le 10 ]
do
echo $counter
((counter++))
done

until [ <some test> ]
do
<commands>
done

# Files & Variables
dirname=$(dirname "$1")
#dirname="$1%/*" == same!
file=$(basename "$1")
extension="$file##*."
filename="$file%.*"

# string comparison
if [ "$extension" != "pdf" ]; then
	echo "Not a PDF file!"
	exit
fi

# Exit codes
tricky-cmd || exit 1


# Arguments
echo $0 $1 $2 $3
echo $@ # all arguments
echo Number of arguments passed: $#
args=("$@") # into array
echo $args[0] $args[1] $args[2]

echo $? # exit code of last command
echo $$ # process id of current process

sleep 0.1s

echo -e "Hallo Welt\n" # -e interprets special characters (e.g. \a alert; \xHH hex)

# Read User input
echo -e "Hi, please type the word: \c "
read  word

# Variables
echo "Hallo $Welt"
echo 'Hallo $Welt' # No substitution

export var1 # Make the variable var1 available to child processes.

## Variable Manipulation
filename=$(basename "$fullfile")
extension="$filename##*."
filename="$filename%.*"

$FILE:4:8 # start at 4 length 8

# Specials
A=`uname -o`
A=$(uname -o)
$(date +%Y%m%d)

date -d @$UNIX_TIMESTAMP

# ARITHMETICS
let a=5+4 # simple arithmetic
let "a=5+4"
let a++/--

a=$(expr 5 \* 4) # prints instead of saves
expr 5 + 4 # SPACES needed

a=$(( b + 5 )) # $b is also valid

$#variable # length of variable

$(date +'%m/%d/%Y')


#file=$(basename -- "$1")
#path=$(dirname -- "$VAR")
filename="$1##*/"
echo "File and extension: $filename"
pathname="$1%/*"
echo "Path: $pathname"
extension="$filename##*."
echo "Extension: $extension"
filename="$filename%.*"
echo "Filename: $filename"

shbash片段(代码片段)

查看详情

shbash片段(代码片段)

查看详情

shbash的代码片段(代码片段)

查看详情

shbash命令(代码片段)

查看详情

shbash日期(代码片段)

查看详情

shbash参数(代码片段)

查看详情

shbash编程(代码片段)

查看详情

shbash脚本(代码片段)

查看详情

shbash标题(代码片段)

查看详情

shbash常见(代码片段)

查看详情

shbash别名(代码片段)

查看详情

shbash功能(代码片段)

查看详情

shbash脚本(代码片段)

查看详情

shbash脚本(代码片段)

查看详情

shbash别名(代码片段)

查看详情

shbash列表(代码片段)

查看详情

shbash历史(代码片段)

查看详情

shbash色表(代码片段)

查看详情