07 String - kumar159man/MyShellLearning GitHub Wiki

Strings

How to get length of a string

Use # to get variable name

myubuntu@myubuntu-VirtualBox:~$ c="CHELSEA"
myubuntu@myubuntu-VirtualBox:~$ echo "Length of the string is ${#c}"
Length of the string is 7

Concatenation of strings

myubuntu@myubuntu-VirtualBox:~$ c="Chelsea"
myubuntu@myubuntu-VirtualBox:~$ d=" is a dog"
myubuntu@myubuntu-VirtualBox:~$ echo "$c$d"
Chelsea is a dog
myubuntu@myubuntu-VirtualBox:~$ e=$c$d
myubuntu@myubuntu-VirtualBox:~$ echo "$e"
Chelsea is a dog

Converting a string to uppercase

myubuntu@myubuntu-VirtualBox:~$ c="chelsea"
myubuntu@myubuntu-VirtualBox:~$ echo "${c^^}"
CHELSEA

Converting a string to lowercase

myubuntu@myubuntu-VirtualBox:~$ echo "${c,,}"
chelsea

Commands dirname and basename

dirname and basename commands are very important while working with paths

  • dirname-> removes file name from directoy
  • basename-> gives only file name
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ dirname /home/myubuntu/Desktop/shellScripts/cut.txt
/home/myubuntu/Desktop/shellScripts
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ basename /home/myubuntu/Desktop/shellScripts/cut.txt
cut.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ d=$(dirname /home/myubuntu/Desktop/shellScripts/cut.txt)
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ e=$(basename /home/myubuntu/Desktop/shellScripts/cut.txt)
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "$d"
/home/myubuntu/Desktop/shellScripts
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "$e"
cut.txt

Slicing of a string

slicing can be done through cut command

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ d="Chelsea"
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo $d | cut -c 1,3
Ce
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ c="Chelsea"
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "${c:0}"
Chelsea
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "${c:1}"
helsea
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "${c:2:1}"
e
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "${c:2:3}"
els

Multi-line comments

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat third.sh 
#!/usr/bin/bash
source ./variables.txt
. ./first.sh
echo "Value of x is $x"
echo "Value of y is $y"
echo "Value of dog is $dog"

<<mycomment1
echo "Comment line1"
echo "Comment line2"
echo "Comment line3"
mycomment1
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./third.sh 
Hello this is my first shell script
Value of x is 18
Value of y is 20
Value of dog is Chelsea
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ 

shell script to install tomcat

Applying these concepts and creating script for installing tomcat tomcat installation script

⚠️ **GitHub.com Fallback** ⚠️