sh Bash控制序列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Bash控制序列相关的知识,希望对你有一定的参考价值。

#!/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"

以上是关于sh Bash控制序列的主要内容,如果未能解决你的问题,请参考以下文章

sh Bash:迭代序列:for,seq

sh 通常,它建议为bash提供复杂提示的方法是内置读取,可能包括转义序列和shell变量

sh [Plex CLI]一个简单的bash脚本来控制您的Plex客户端#plex #cli #shell

FFmpeg 如何使用 bash 生成视频序列

从 bash 脚本运行多个命令而不会失去控制

Chapter_1_part_1