Interactive from keyboard (user):
read -p "Message" var1 var2 ... varN
read -p "What is your name? " NAME
echo "Hello $NAME."
read -p "What is your name? " FIRST LAST
echo "Hello $LAST $FIRST."
read -p: Show a message (prompt)read -a: Assign words to arrayread '-d ': Set other delimiter (e.g. space)read -t n: Timeout after n seconds See: $ help read or this article on computerhope.com
“A string treated as a list of characters that is used for field splitting, expansion of the ’*’ special parameter, and to split lines into fields with the read utility."
Changing the separator to ‘,’ (comma):
LIST="one,two,three"
IFS=','
for ITEM in $LIST; do
echo "item: $ITEM"
done
…but it could be any character! :)
Reading a textfile line by line:
I=1
while IFS= read -r LINE
do
echo "$I: $LINE"
I=$((I + 1))
done < mylist.txt
mylist.txt:
one
two something: \"
three and additional
four
Just add an ampersand at the end:
$ my_program &
Ctrl+Z: Suspend Jobfg [JOB_ID]: Move to foregroundbg [JOB_ID]: Run in backgroundjobs: List background jobsffmpeg -i day.ts -f segment -segment_time 3600 \
-c:a copy out_%02d.ts
for FILE in out*.ts; do
MP3_OUT=$(basename "$FILE" .ts).mp3
# Run FFmpeg separately for each segment.
# In the background.
ffmpeg -i $FILE -c:a mp3 -b:a 192k MP3_OUT &
done
…or GNU-parallel-ize it!
ls *.ts | parallel ffmpeg -i {} -c:a mp3 -b:a 192k mp3/{}.mp3
# If 'first' succeeds, 'second' will never be executed:
$ first || second
# Only run 'second' if 'first' is successful:
$ first && second
Including/importing code from other files:
source functions.sh
# read_fps() is declared in functions.sh:
read_fps "$VIDEO_IN"
while vs for?eval?<, | and > for?&& and || mean?It’s your turn!