intro shell bash scripting
Konstantinos Blatsoukas
Posted on October 16, 2024
writting a simple script
#~/bin/bash
cd /etc
cat ./passwd
give execution permissions
chmod +x test_script.sh
pass data
#~/bin/bash
echo good morning $1 I hope you haveiong a great day
Environmental variable
# craete an environmental variable
export USERNAME=kostas
#~/bin/bash
echo good morning $USERNAME I hope you haveiong a great day
zip a file and move it to a new directory
#!/bin/bash
source=$1
dest=$2
tar -cvf backup.tar $source
gzip backup.tar
mv backup.tar.gz $dest
mkdir audiofiles
cd audiofiles/
touch f1 f2 f3
cd ..
READ and TEST commands, if conditionals
#!/bin/bash
# read user input
read -p "enter your desired username " username
echo you have chosen $username as your username
$?
# 0 success, 1 and above error
#!/bin/bash
if [ 5 -gt 3 ]; then
echo yes it is greater than 5
else
echo no it is not
fi
loops
#!/bin/bash
for name in natthew mark luke
do
echo good morning $name
done
loops using a bash command
#!/bin/bash
count=1
for file in `ls *.log`
do
echo "log file number $count: $file"
count=$((count+1))
done
loops, the while case
#!/bin/bash
while true
do
touch file-`date +%s`.txt
sleep 3
done
loops, the until case
#!/bin/bash
until `ssh 192.168.1.100`
do
echo "still attemting..."
done
functions in Bash
mkdir newdir
cd newdir
touch file1 file2 file3 file4
#!/bin/bash
source=$1
dest=$2
function fileExists() {
# check file existence
if [ -f $dest/backup3.tar.gz ]; then
echo this file already exists
exit
fi
}
function compressDir() {
tar -cvf backup3.tar $source
gzip backup3.tar
mv backup3.tar.gz $dest
}
fileExists
compressDir
💖 💪 🙅 🚩
Konstantinos Blatsoukas
Posted on October 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024