Eval Command in Linux

Eval is built in unix command – it’s used to execute arguments as shell commands. It’s useful when you have command stored in variable and you want to execute it.

[root@server Files]# mycommand="ls -ltr"

[root@server Files]# echo $mycommand
ls -ltr

[root@server Files]# eval $mycommand
total 0
-rw-r--r-- 1 root root 0 Nov  5 07:40 file1.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file3.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file2.txt
[root@server Files]#

Look at the example above, I have stored the command ls – ltr in the variable mycommand so when I do echo it’s just prints what in the variable. But when I do eval followed with the variable – it will actually execute the command.

Eval is powerful command but it’s Evil – it has security issues as well.
You can find the details here

Exit Status in Linux.

What is exit status in the linux?

Whenever you fire a shell command it returns a status to check if that command was successful or not – exit status can be very useful in scripts to make you scripts stable.

Example:-

In the example below, I ran uptime command and it was successful – so when I checked the exit status using echo $? it showed 0.

[root@server ~]# uptime
 05:15:27 up 50 days, 16:53,  1 user,  load average: 0.08, 0.03, 0.08

[root@server ~]# echo $?
0
[root@server ~]#

Now let’s see what happens when command is not successful.

So in the example below, I typed incorrect command and exit status was other than 0 – which indicates error.

[root@server ~]# uptimee
-bash: uptimee: command not found

[root@server ~]# echo $?
127
[root@server ~]#

Practical usage in shell scripting.

#!/bin/bash
cd $1 > /dev/null 2>&1

if [ $? != 0 ]
then
        echo "Directory Not Found" ; exit
fi

echo "Great!"

So, above script terminates if cd command is not successful.
If it’s successful it will execute further code.

[root@server ~]# ./exitstatus.sh /tmp
Great!

[root@server ~]# ./exitstatus.sh /tmpp
Directory Not Found
[root@server ~]#

Avoid Confirmation while Copying Files

While copying files, using cp command you would have noticed that it always asks for the confirmation overwriting files like below.

[root@server ~]# cp -v test.txt example.txt
cp: overwrite ‘example.txt’?

Will see how you can avoid that – but before that let’s see why it happens.

This happens because of alias set in the .bashrc file.

[root@server ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

you can remove alias in .bashrc file – and you won’t be asked for confirmation ever again.

but if you want to skip confirmation for time being then you can call the cp command directly from bin – Example.

[root@server ~]# /bin/cp -v test.txt example.txt
‘test.txt’ -> ‘example.txt’
[root@server ~]#

Rsync command examples

So what is Rsync?
Rsync is a Linux tool which helps is syncing a file from a server A to server B and vice versa. Rsync is powerful tool which numerous options and we will see some of the examples below.

Rsync Command to copy files in the destination.

So the basic command would look like this.

[root@server ~]#  rsync [option] user@host:[file/directory origin] [destination]

[root@server ~]# rsync -avzh /home/user/backups/ user@backup01:/home/backup/

So above command will copy contents in the backups folder to the remote destination – Important note if you don’t put trailing / in the /home/user/backups it will copy whole folder instead of just contents in it.

Further to explain above command:-
-a stands for copy files recursively and preserve ownership of files when files are copied
-v stands for runs the verbose version of the command; all of the processes that are run will be written out for the user to read
-z stands for compress the data synced
– h stands for produce easily readable output, not just numbers

Show Progress:-

If you are doing a large transfers and want to see progress, you can use –progress option.

[root@server ~]# rsync -avzh --progress /home/user/backups/ user@backup01:/home/backup/


Exclude Files/folders from syncing.

Create a file call exclude.txt and add all the files and folders you want to exclude e.g:-
test/file.txt
foldername

rsync -avzh --exclude-from 'exclude.txt' sync/ 5.7.7.2:/home/