Saturday, February 20, 2016

Unix Shell Script Basics

14 04. Unix Shell Scripting Tutorial - Shell Programming Features (Part 3)
###### the types of shell language #####
Bourne Shell --- sh
Ksh
bash
zsh
csh
#### the script interpreters
csh
sed
tcl
perl
awk
##############################
Step 1: edit shell script
Step 2: chmod +x script
step 3: ./script #######to run
##########################
echo $path
################
echo
read varname var2
echo $varname $var2
echo "abc \c"
#######################
ls > a.txt
ls >> a.txt #append the file
########## Error Message ##############
find -name a.txt >result.txt 2>error.txt
find -name a.txt >result.txt 2>&1 #save to same file
#######################
cat file
######### number of users on system #####
echo passwd > file.txt
## number of logged users ###\
echo who
## number of running processes ##
ps -e
## count numbers of user
cat /etc/passwd | wc -l
###############################
=========
### run in background ##
&
sleep 100 &
backup &
#######################
< stardard input
who | wc > user.txt
#######################
sudo add-apt-repository ppa:notepadqq-team/notepadqq
sudo apt-get update
sudo apt-get install notepadqq

#######################Special Character#######################
& * ? [ ] < > |
( ) ' # $ ^ =
` ' " { } ; \"
#######################
Echo the fee is \$10
echo the comment is "#" character
echo "the comment is # character"
echo the comment is `$` character
####################### Comments #######################
##run a script
1. myscript
2. sh myscript ### the script does not need to be made executable
3. . myscript  ### the script are excuted by current shell
4. sh .profile
5. . . profile
6. exec myscript ## terminates current shell, run script, once done, logged off current user

############ Specify Script Interpreters ##########################
#!/bin/csh  ### in first line of script, specify which interpreter
OR
#!/user/local/bin/perl
######################## Variables #######
month=agust #### case sensitive
sports=basketball ## no space
street="Smith Avenue"  ## add space
###### all shell variables are strings
echo you live on $street
############ Environment Variables #########
$HOME
$PATH
$LOGNAME
$TERM
## change the enviroment variable , then execute
. SCRIPT
SH SCRIPT
## export variable to environment
## 1. script.sh ##
month=Jan
export month
## 2. run it ##
. script.sh
## #########
echo $month
## ############# Quotes ######################
' single quote ##### seperate command parameters, new line characters
" dobule quote #####
` back quote











No comments:

Post a Comment