Array in Shell Script
Shell script is powerful if you could remember all the weird syntax.
Here is the example to show how to use Array in Shell script.
Note: readarray only works in Bash 4.x, check your version: bash --version
string='Paris, France, Europe';
echo $string
readarray -td, array <<<"$string"; declare -p array;
## declare -a array=([0]="Paris" [1]=" France" [2]=$' Europe\n')
country='Paris/Frence/Europe/Sweden/Croatia';
echo $country
readarray -td/ array <<<"$country"; declare -p array;
for item in "${array[@]}"
do
echo "$item"
done
Shell Script loop
for i in *; do
echo $i
done
for i in *; do echo $i ; done
# copy all file name with pattern debug to $w/zsurface/image
for i in $(ls | grep html | awk '{print $1}'); do cp $w/zsurface/image; done
# print file name in ls -lah
awk '{print $9}'
gsed, sed replace text in many files
# replace oldstring with newstring for all html file
# and backup all the files with suffix _backup
gsed -i_backup 's/oldstring/newstring/' *.html
Compare Number in Fish Shell
if math "$SHLVL == 1"
read -p 'echo "Exit? (y/n):"' -l confirm
if [ $confirm = "y" ]
exit
end
else if math "$SHLVL > 1"
exit
end
Customize Prompt in Fish Shell
function fish_prompt
set_color green
echo -n (pwd)"[$SHLVL]"'>'
end
Compare String in Fish Shell
if [ $confirm = "y" ]
exit
end