Programming in Shell Script - 1

TheFlow

Üye
19 Şub 2021
202
1
Shell Scripts-1​


Hi! today I'll show you how to program Shell Scripts with examples of codes. Let's get started!
First of all, im in OS based on linux. We need to do things before go on.​


Kod:
1)cat /etc/shells     # This command shows you what is scripting language of your OS.
  2)which bash         # this command allows us to find where the bash is 

3)touch hello.sh          # Create a file in desktop( or anywhere you want) 

4)ls -al hello.sh      # This command shows you what permissions file has. 

5) chmod u+x hello.sh  # In this command, we are giving permissions to run our code.
 6)nano hello.sh       # Opening file in nano text editor.
 7) ./hello.sh          # Command to execute our program.
After these processes, we are now ready to write our code
Lets say "Hello world" first.


Kod:
  #!/bin/bash ->We need to write this line. 

 #  -> Used to write a comment line. 

echo "Hello world" ->Print "Hello world" to the screen.
In a linux based OS, There is a tow type of variables. One is system variables and other is user defined variables.
If a variable consists of uppercase letters, then most likely - but not required - it's a system variable. Let's take a look with an example;



Kod:
#!/bin/bash 

echo "Our shell's name: $BASH" 

echo "Our Shell's version: $BASH_VERSION" 

echo "Our main directory: $HOME" 

echo "Current working directory: $PWD" 

#Executes system commands and prints the screen 

name="Sia" #In here, we are defining a variable.
  echo $name -> for printing the variable,we put $ in the beginning 

 echo "name is: $name" -> You see printing the variable with text
Let's get input from user



Kod:
#!/bin/bash  echo "please give more than one name: "
  read name1 name2 name3                    #-> In this line, we both define variables and reading them 

echo "names: $name1, $name2, $name3"    #-> we print the names we received on the screen
Note, however, that the default separator character is space, so you cannot give more than three strings to the example above. For example, this "a string with more than 3 words" is actually resolved as three strings. That's the first one. Second. And the rest of the string is enclosed in quotation marks, so it is interpreted as a single string and assigned to the variable "name3". You can also combine an echo expression with a reading expression through the -p option.​


Kod:
#!/bin/bash 

 read -p 'username: ' user_name 

read -sp 'password: ' user_pass 

echo "Username is $user_name" 

echo "Password is $user_pass"
In the example above, "read" first prints the quoted string, then waits for the input from the user. You can also hide the input string when the user writes with the "-s" option. You can also get a large number of en inputs from the user with the "-a" option. -an option parses the given string and places all space-separated words as an array in the specified variable.



Kod:
#!/bin/bash 

echo "give me name: " 

read -a names 

echo "Names: ${names[0]}, ${names[1]}, ${names[2]}"
If the read command is called without any arguments, the input of the command can $REPLY through the variable "Cannot be used".

When running ./hello.sh at the command line, the following command allows us to get input before the program starts.

Kod:
 #!/bin/bash

# 3 arguments are received from the user
echo $1 $2 $3

echo "command= $0" -> Comand equals to index of 0
#when we'll run program. ./nameofprogram.sh make us run the program as name1 name2 name3, and the 0 index=./nameofprogram.sh equals the program.

# in this section the same process is done by being thrown into the list
args=("[email protected]") - >list
echo ${args[1]} ${args[2]} -> Making a list
echo [email protected] -> print the whole list to us
 echo $# -Prints > printing the arguments in the list
 ,


 
Son düzenleme:

THT SON MESAJLAR

Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.