list of some common settings, numerical values and their meanings:
-rw------- (600) -- Only the user has read and write permissions.
-rw-r--r-- (644) -- Only user has read and write permissions; the group and others can read only.
-rwx------ (700) -- Only the user has read, write and execute permissions.
-rwxr-xr-x (755) -- The user has read, write and execute permissions; the group and others can only read and execute.
-rwx--x--x (711) -- The user has read, write and execute permissions; the group and others can only execute.
-rw-rw-rw- (666) -- Everyone can read and write to the file. Bad idea.
-rwxrwxrwx (777) -- Everyone can read, write and execute. Another bad idea.
----------
Variables are memory location which can store some values.
syntax: <variable_name>=<variable_value>
eg: MYNAME=devops_user
How to print / check / substitute / call values of a variable?
--------------------------------------------------------------
$ (dollar) is mandatory while substituting varaible ==> $<variable_name>
echo $<variable_name>
ex: PLAYER=viratKohli
echo " my favorite sportsmen is $PLAYER "
Variable types:
---------------
1. Local variables / user defined variables.
variables created by user
2. system / predefined variables.
variables created by system
env ==> command to check system defined variables
user defined variables are only valid till your terminal(session) exists or your servers running.
to store variables irrespective of terminal running or restarts
How to set variable permanately in all shell sessions & also server restarts ?
------------------------------------------------------------------------------
Answer: By adding the variable inside startup script ( ~/.bashrc ).
*******IMPORTANT********************
EXIT CODE
-------------
every command /script generates an auomatic code which signifies, previous command execution is succesfull or not
syntax: echo $?
if output is 0 zero ==> command executed succesfully
if output is non-zero ==> Command not executed succesfully
SHELL:
------
1 Shell is responsible to read command provided by user.
2. Shell will check whether the command is valid or not.
3. Shell interpretes (converts) that command into kernel understandable form & Kernel execute that command with the help of hardware.
Shell + kernel = Linux OS.
Types of shell:
---------------
1. sh ==> basic shell, earlier / olden days is used in unix ==> /bin/sh
2. bash ==> adavnced shell, which is used in linux ==> /bin/bash
others: c-shell, k-shell etc
Note: echo $SHELL ==> Command to check which shell we are using
Steps to create & execute shell script:
----------------------------------------
step1. shell script will have extension called <script-name>.sh
vi <script-name>.sh
step2. in the script first line we are going to write is called as shebang #!/bin/bash
step3. you add your commands (save the script file)
step4. execute the script
sh <script-name>.sh (or) bash <script-name>.sh (or) ./<script-name>.sh
what is shebang line, why its sused?
------------------------------------
shebang line is used to specifies interpreter (or) shell to be used for execution of script
can we run script without shebang line?
------------------------------------------
yes, in that case whatever default shell ia there, that will get used.
script1. write a shell script to print date & time.
---------------------------------------------------
vi date-script.sh
#!/bin/bash
echo "Current date and time...."
date
save & quit
execute script
sh date-script.sh
script2. write a shell script to CHECK Storage(HDD) of server.
--------------------------------------------------------------
vi STORAGE-script.sh
#!/bin/bash
echo "Current storage used is ...."
df -kh | head -5 | tail -1 | awk -F " " '{print$5}'
save & quit
execute script
sh STORAGE-script.sh
script3: write a script to create a variable (sportsman=viratkohli) & verify the variable creation using echo command:
----------------------------------------------------------------------------------------------------------------------
[ec2-user@ip-172-31-6-171 ~]$ cat vars_check.sh
#!/bin/bash
sportsman=viratkohli
echo "my fav sportsamen is $sportsman"
script4: write a script to have employee data coming as variables & verify the variable creation using echo command:
----------------------------------------------------------------------------------------------------------------------
[ec2-user@ip-172-31-93-35 ~]$ cat employee_data.sh
#!/bin/bash
EMP_NAME=alice
EMP_ID=54234
EMP_LOC=bangalore
EMP_STATE=karnataka
echo "Display employee name $EMP_NAME "
echo "Display emloyee id $EMP_ID "
echo "Display employee location $EMP_LOC "
echo "Display employee state $EMP_STATE "
script5: script to check harware resources:
-------------------------------------------
[ec2-user@ip-172-31-6-171 ~]$ cat ram_usage_check.sh
#!/bin/bash
echo "Current free ram available is..."
free -m | grep Mem | awk -F " " '{print $4}'
#free -m
echo "current disk usage is..."
df -kh .
echo "number of cpu...."
nproc
Good One Rajeev, however you have a catch in the code. Guess what? its the time stamp. all have same.. we have defined the timestamp initially and used it for all.
-rwxrwxrwx 1 root staff 153 Sep 25 22:59 Rajeev.sh
-r--r--r-- 1 root staff 0 Sep 25 22:59 file12024.09.25-22.59.30.log
-r--r--r-- 1 root staff 0 Sep 25 22:59 file22024.09.25-22.59.30.log
-r--r--r-- 1 root staff 0 Sep 25 22:59 file32024.09.25-22.59.30.log
-r--r--r-- 1 root staff 0 Sep 25 22:59 file42024.09.25-22.59.30.log
-r--r--r-- 1 root staff 0 Sep 25 22:59 file52024.09.25-22.59.30.log
Task Updated.
Dinesh Interestingly you have used Seq , curious to know why you used Seq, also i see only the date and not timestamp....
-rwxrwxrwx 1 root staff 151 Sep 25 23:01 Dinesh.sh
-r--r--r-- 1 root staff 0 Sep 25 23:01 file12024-09-25.log
-r--r--r-- 1 root staff 0 Sep 25 23:01 file22024-09-25.log
-r--r--r-- 1 root staff 0 Sep 25 23:01 file32024-09-25.log
-r--r--r-- 1 root staff 0 Sep 25 23:01 file42024-09-25.log
-r--r--r-- 1 root staff 0 Sep 25 23:01 file52024-09-25.log
Assignment Task
Good that you have printed the message , However the timestamp is not formatted properly. Good Try!
Also need to think about using loops, the program looks raw.
The 5 files are created with timestamp successfully
The file permissions are modified with read-only for all the users successfully.
-rwxrwxrwx 1 root staff 424 Sep 25 23:03 Muruguraj.sh
-r--r--r-- 1 root staff 0 Sep 25 23:04 file11727285646.log
-r--r--r-- 1 root staff 0 Sep 25 23:04 file21727285646.log
-r--r--r-- 1 root staff 0 Sep 25 23:04 file31727285646.log
-r--r--r-- 1 root staff 0 Sep 25 23:04 file41727285646.log
-r--r--r-- 1 root staff 0 Sep 25 23:04 file51727285646.log
Task Completed.
I see you have done it, pls thing about using loops.. Good Try.. pls start thinking more like programmer.
-rwxrwxrwx 1 root staff 275 Sep 25 23:05 Anand.sh
-r--r--r-- 1 root staff 0 Sep 25 23:05 file11727285754.log
-r--r--r-- 1 root staff 0 Sep 25 23:05 file21727285754.log
-r--r--r-- 1 root staff 0 Sep 25 23:05 file31727285754.log
-r--r--r-- 1 root staff 0 Sep 25 23:05 file41727285754.log
-r--r--r-- 1 root staff 0 Sep 25 23:05 file51727285754.log
Task Completed
Assignment completed.
@Jeeva - Not getting expected output, pls try it again
#!/bin/bash timestamp=$(date +%s); #create the files touch file1$timestamp.log file2$timestamp.log file3$timestamp.log file4$timestamp.log file5$timestamp.log previous=$? if [ $previous -eq 0 ] then echo "Files are created successfully" fi #Change the files Permission chmod 444 file1$timestamp.log file2$timestamp.log file3$timestamp.log file4$timestamp.log file5$timestamp.log previous=$? if [ $previous -eq 0 ] then echo "read only permissions updated to all the files" fi
Karthik you have some interesting stuff, however your program is not scalable and bit raw. Good Try.
-rwxrwxrwx 1 root staff 484 Sep 25 23:13 Karthik.sh
-r--r--r-- 1 root staff 0 Sep 25 23:16 file11727286371.log
-r--r--r-- 1 root staff 0 Sep 25 23:16 file21727286371.log
-r--r--r-- 1 root staff 0 Sep 25 23:16 file31727286371.log
-r--r--r-- 1 root staff 0 Sep 25 23:16 file41727286371.log
-r--r--r-- 1 root staff 0 Sep 25 23:16 file51727286371.log