Bash How to Return from Function

Bash How to Return from Function

Use the statement “return” to return from the function. You can also specify the return value. Example:

return 1234

We returned function status 1234 from function. Usually, we use 0 value for success and 1 for failure. It is similar to command exit which we use to terminate the script.

If you don’t use the return statement in the whole function, the status of the last executed command will be returned.

To verify returned status from the last called function use “$?”.

There is a difference between commands return and exit. The exit will cause the script to end at the line where it is called. Return will cause the current function to go out of scope and continue execution command after the function.

Bash How to Read from Keyboard

Bash How to Read from Keyboard

To read input from the keyboard and assign input value to a variable use the read command.

Read syntax:

read options var1 var2

To write the value of first entered word use:

read var1 var2
echo $var1

If you don’t give any argument to the read command, input will assign to the environment variable REPLY.

The “s” option does not echo input while reading from a keyboard.

read -s -p "Enter password:" $password

The -p “TEXT” option displays TEXT to the user without a newline.

The -e option means that command readline is used to obtain the line.
Option -u FD reads inputs from file descriptor FD (0,1,2).
Option -t TIME causes that read returns a failure if the input is not read within TIME seconds.

Linux: How to Connect External Hard Drive

Linux: How to Connect External Hard Drive

Diagnostic Commands

Display from log directory what was recently connected

# tail -20 /var/log/messages

Display list of the partition tables

# fdisk -l

Display device parameters

# hdparm /dev/sda

More detailed information

# hwbrowser

# more /etc/sysconfig/hwconf

Connection Process

Run fdisk

# fdisk /dev/sda

Get list of existing partitions

Command (m for help): p

Delete partition which exist on the drive

Command (m for help): d

Create new partition

Command (m for help): n

Verification before save

Command (m for help): p

Save new table to drive

Command (m for help): w

Update partition table in system

# partprobe

Alternative to partprobe is hdparm -z

# hdparm -z /dev/sda

Format created partition to ext3 file system

# mkfs.ext3 /dev/sda1

Verification if the partition is not mounted

# df -hT

Mount new-created partition to directory /mnt/usb. If a directory does not exist, first created it.

# mkdir /mnt/usb

# mount /dev/sda1 /mnt/usb

# df -hT

Display disk partitions

# parted /dev/sda unit GB print free

How to Install Software in Linux

How to Install Software in Linux

Shared libraries

Print command dependencies of shared libraries

# ldd /bin/ls

Add new shared library to program icq

# ldconfig -n /opt/icq/lib

Actual shared library in cache (/etc/ld.so.cache)

# ldconfig -p

Get settings of shared libraries

# cat /etc/ld.so.conf.d /etc/ld.so.conf.d/*

RPM Package Manager (RPM)

Install

# rpm -i package.rpm

Uninstall (erase)

# rpm -e package.rpm

Upgrade

# rpm -U package.rpm

Upgrade only if an older version is installed

# rpm -F package.rpm

Print list of all installed packages

# rpm -qa

Find verbose information about package

# rpm -qv package

Print information about a package

# rpm -ql package

Display only the config files in package

# rpm -qc package

Display install destination folder before installation

# rpm -qpl package.rpm

Print changelog before installation

# rpm -qp --changelog package.rpm

Print name of package which binds to file

# rpm -qf /etc/exports

Integrity verification of all packages

# rpm -Va

Debian Package (dpkg)

Install

# dpkg -i package.deb

Uninstall (remove)

# dpkg -r package

Uninstall (include config files)

# dpkg -r --purge package

Display package’s files content

# dpkg -c package.deb

Display status of specified package

# dpkg -s package.deb

List files installed to your system from the package

# dpkg -L package

Print partially installed packages and give suggestions on how to fix them

# dpkg -C package

List of all installed packages

# dpkg -l

Source code

Extraction

# tar –xzf /path/name/package.tar.gz

# tar –xjf /path/name/package.tar.bz2

Prepare for compile

# ./configure

Start compile

# make

Install

# make install

NANO Tutorial for Linux

NANO Tutorial for Linux

Start nano:

nano textFile.txt

Show help:

Ctrl + G

Hide help:

Ctrl + X

Save file:

Ctrl + O

Save and exit:

Ctrl + X then answer Y

Exit without saving:

Ctrl + X then answer N

Go to previous/next line:

Ctrl + P/Ctrl + N

Go to previous/next screen:

Ctrl + Y/Ctrl + V

Go to the line and column number:

Ctrl + _

Insert another file into the current file:

Ctrl + R, enter path to the file

Search for string or regular expression:

Ctrl + W

Cut the current line and store it in the cutbuffer:

Ctrl + K

Uncut from the cutbuffer to the current line:

Ctrl + U

Copy the current line to cut buffer:

Alt + 6 or Alt + ^

Linux How to Zip

Linux How to Zip

To create a compressed file and save disk space use following the following syntax:

zip options name.zip file1 file2 file3 folder

The zip program compresses one or more files into the name.zip archive. Zip has options from “1” to “9”. Option -1 use for fast compression and -9 for better compress ratios.

zip -9 name.zip file1 file2

If you want to insert a new file to the existing zip archive use options “u” which means update.

zip -u name.zip newFile

To decompress the zip archive use command:

unzip name.zip

To decompress to a specific directory use the “d” option:

unzip name.zip -d /directory

Command tar is a great tool too. You can compress files with tar:

tar -zcvf name.tgz file1 file2 file3 folder

To unzip compressed file type:

tar -zxvf name.tgz

Linux How to Change Password

Linux How to Change Password

If you want to change password of user that are currently logged in, just type:

passwd

Input old password, then type new password 2 times.

Did you run the passwd command as root? There is a difference between whether you change the password as an root or as a standard user. While the root is called upon to enter a password, the password policy more fully. But if he fails, the password is changed, even though the system grumble. Current user must comply with password policy.

Let’s assume, that you are user bob. If you would like to change bob’s password, log as bob and type:

passwd

If you are root, and if you want to change bob’s password, type:

passwd bob

Please, always try to check, if you set good new password. There is a possibility, that you misspell the password, so try to log in to second console (or putty session) immediately after login.

You can also set the following options:

Option Property
-d Delete password for an account. It will set a passwordless account.
-e Expire. The user will be forced to change the password in the next login.
-n Minimum password lifetime.
-x Maximum password lifetime.

 

JOE Tutorial for Linux

JOE Tutorial for Linux

Start joe:

joe ./textFile.txt

Show help:

Ctrl + K + H (to hide help, enter this command again)

Save file

Ctrl + K + D

Save and exit:

Ctrl + K + X

Exit without saving:

Ctrl + K + Q

Go to previous/next screen:

Ctrl + U/Ctrl + V

Move cursor to the start/end of file file:

Ctrl + K + U/Ctrl + K + V

UnDo recent change:

Ctrl + Shift + _

ReDo undone change:

Ctrl + Shift + ^

Insert or overwrite:

Ctrl + T

Selecting text:

Ctrl + K + B (block begin)
Ctrl + K + K (block end)

When you have block selected, to move block using:

Ctrl + K + M

Delete block:

Ctrl + K + Y

Copy block:

Ctrl + K + C

Search in the file:

Ctrl + K + F

Then choose between ignore (I), replace (R) options. To navigate to next result use:

Ctrl + L

If you want to change joe’s default setting use:

Ctrl + T

You can turn on/off auto-indent, word wrap, line numbers, highlighting and set tab width, left margin, etc.

Linux How to Find File by Name

Linux How to Find File by Name

To search for files on the disk, you can use the find command. The find command has the following syntax:

find /where_to_start -name "name_of_file"

If you do not mention the parameter /where_to_start, it will automatically search in the current directory. The current directory, which you currently stand, is available by typing pwd command. The second parameter -name “name_of_file” – is shown filter. This filter shows only those files in which there is a string “name_of_file”. You can also include an asterisk, for example: “name_of_file.*”.

If you want to recursively overview /etc directory and find all files that have the extension “.conf”, you can do it this way:

find /etc -name "*.conf"

If the find command does not access to any folder, it write error about it. If you do not run the command as super user, it is better to redirect error messages to $HOME/find_errors or to trash /dev/null.

In next example we redirect errors to file find_errors that will be situated in our home folder:

find /etc -name "*.conf" 2> $HOME/find_errors

In next example we redirect errors to the system trash:

find /etc -name "*.conf" 2> /dev/null

To find file by a name, ignoring the case use option -iname:

find /etc -iname "name_of_file"

If you want to find all files these don’t match the pattern:

find /etc -not -name ".*.conf"

Linux How to Remove Directory

Linux How to Remove Directory

To remove an empty directory use the command:

rmdir directory/

To remove a directory which contains files or sub-directories use this way:

rm -r not-emty-directory/

If you want to ignore nonexistent files and never prompt, use the option “f”:

rm -rf not-empty-directory/

Command rm has option “v” which explains what is being done.

rm -rv directory

If you don’t have permision to delete folder add sudo at the beginning:

sudo rm -rv directory

If you are not 100 % sure if you won’t remove all the files in directory, use -i option:

rm -rvi directory

Shell will ask you before removing each file.

Linux Find String in Folder

Linux Find String in Folder

In our examples, we assume, that we want to find the name of the network card “eno16777728” in /etc folder recursively.

This example search “eno16777728” in /etc folder (-r means recursive, -n means print line number):

grep -nr "eno16777728" /etc

In next example, we would like to add ignore case with “i” option:

grep -inr "eno16777728" /etc

If you are not super user, it is good idea to suppress error messages with “s” option:

grep -insr "eno16777728" /etc

In examples mentioned before, searched string could be also a part some string. In next example, we would like to find “eno16777728” as whole word only (w option):

grep -winsr "eno16777728" /etc

The alternative way is use find and exec option:

find /etc -type f -exec grep -il 'eno16777728' {} \;

In this case option exec executes grep command for each founded file. In {} is a list of founded files.