Two repositories of Linux and Unix commands and example dot files
$ ls -a
Read more!
Tips and Tricks in using and administering Linux.
Also contain News, Linux books reviews, OS reviews, Linux desktop, Linux devices and much more. Has over 600 unique articles with more added each day. If you like this blog, please support by providing a link to it.
$ ls -a
Posted by
Pat
4
comments
Labels: bash shell, commands
#!/usr/bin/bash
$ which bash
/usr/bin/bash
# Continuation of the above script ...
# Now lets enter a few commands in this script ...
ls -l
sleep 2
who
sleep 2
w
$ chmod u+x myfirstscript.shThis lets you execute the script by its name as follows :
$ ./myfirstscript.sh
Posted by
Ravi
4
comments
Labels: bash shell, python, scripting, tips
Posted by
Ravi
3
comments
Labels: bash shell, tips
Posted by
Ravi
1 comments
Labels: bash shell, benchmarks
Posted by
Ravi
0
comments
Labels: bash shell, tips
Posted by
Ravi
1 comments
Labels: bash shell, system administration
In the past, I have written an article on this blog titled - 10 seconds guide to Bash Scripting - which explained the finer nuances of learning to write scripts for the bash shell. But there is much more to it than scripting for one particular shell as there are numerous shells available for the Linux/Unix environment.
Posted by
Newsguy
0
comments
Labels: bash shell, external links
$ echo $IFS | od -bc$ ls |... and press enter.$ echo $USER
ravi... I get the value stored in the environment variable USER.#!/bin/bash $ chmod ugo+x your_shell_script.sh if condition_is_true
then
execute commands
else
execute commands
fi if condition_is_true
then
execute commands
elif another_condition_is_true
then
execute commands
else
execute commands
fiExample :if grep "linuxhelp" thisfile.html
then
echo "Found the word in the file"
else
echo "Sorry no luck!"
fiif's companion - test-eq Equal to
-lt Less than
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to File related tests-f file True if file exists and is a regular file
-r file True if file exists and is readable
-w file True if file exists and is writable
-x file True if file exists and is executable
-d file True if file exists and is a directory
-s file True if file exists and has a size greater
than zero.String tests-n str True if string str is not a null string
-z str True if string str is a null string
str1 == str2 True if both strings are equal
str1 != str2 True if both strings are unequal
str True if string str is assigned a value
and is not null.Test also permits the checking of more than one expression in the same line.-a Performs the AND function
-o Performs the OR functionExample:test $d -eq 25 ; echo $d... which means, if the value in the variable d is equal to 25, print the value.test $s -lt 50; do_somethingif [ $d -eq 25 ]
then
echo $d
fi
if [ $str1 == $str2 ]
then
do something
fi
if [ -n "$str1" -a -n "$str2" ]
then
echo 'Both $str1 and $str2 are not null'
fi
... above, I have checked if both strings are not null then execute the echo command.case expression in
pattern1) execute commands ;;
pattern2) execute commands ;;
...
esac ...
echo "Enter your option : "
read i;
case $i in
1) ls -l ;;
2) ps -aux ;;
3) date ;;
4) who ;;
5) exit
esac
Note: The last case option need not have ;; but you can provide them if you want.case `date |cut -d" " -f1` in
Mon) commands ;;
Tue) commands ;;
Wed) commands ;;
...
esac
...
echo "Do you wish to continue? (y/n)"
read ans
case $ans in
Y|y) ;;
[Yy][Ee][Ss]) ;;
N|n) exit ;;
[Nn][Oo]) exit ;;
*) echo "Invalid command"
esac
while condition_is_true
do
execute commands
done
Example:while [ $num -gt 100 ]
do
sleep 5
done
while :
do
execute some commands
done
until false
do
execute commands
done
Example:...
until [ -r myfile ]
do
sleep 5
done
The above code is executed repeatedly until the file myfile can be read.for variable in list
do
execute commands
done
Example:...
for x in 1 2 3 4 5
do
echo "The value of x is $x";
done
for var in $PATH $MAIL $HOME
do
echo $var
done
...
for file in *.java
do
javac $file
done
Note: You can use wildcard expressions in your scripts.$* - This denotes all the parameters passed to the script
at the time of its execution. Which includes $1, $2
and so on.
$0 - Name of the shell script being executed.
$# - Number of arguments specified in the command line.
$? - Exit status of the last command.
$ ./my_script.sh linux is a robust OS
$ set `date`
$ echo $1
$ echo $*
$ echo $#
$ echo $2
$ set `date`
$ echo $1 $2 $3
$ shift
$ echo $1 $2 $3
$ shift
$ echo $1 $2 $3
To see the process Id of the current shell, try this:$ echo $$
2667
Validate that it is the same value by executing the following command:$ ps -f |grep bashread statement#!/bin/sh
echo "Enter your name : "
read name
echo "Hello $name , Have a nice day."
Exit status of the last command
Posted by
Ravi
39
comments
Labels: bash shell, system administration
$ dialog --title "Ravi's Input Box"
--inputbox "Enter the parameters..."
8 40 $ dialog --title "textbox" --textbox ./myfile.txt 22 70 ... it shows the file myfile.txt in a textbox.$ dialog --checklist "Choose your favorite distribution:"
10 40 3
1 RedHat on
2 "Ubuntu Linux" off
3 Slackware off
$ dialog --backtitle "Processor Selection"10 and 40 are the height and width respectively. 4 denotes the number of items in the list.
--radiolist "Select Processor type:"
10 40 4
1 Pentium off
2 Athlon on
3 Celeron off
4 Cyrix off
$ dialog --title "Memory Results"
--infobox "`echo ;vmstat;echo ;echo ;free`"
15 85
Labels: bash shell, scripting
# apt-get install bash-completion
$ grep --

Posted by
Ravi
4
comments
Labels: bash shell, system administration
Labels: bash shell