Linux bash tiny-tips

Killin’ the process via its id in Linux:
#ps -aux | grep IVI_ConnectionManager | awk ‘NR==1{print $2}’ | xargs kill

 

Launch two different shell in one script:

#/bin/bash

command_1 && command_2

(Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it’s different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.)

 

Get input repeatedly from the shell:

Value 127 (non-zero) indicates command cyberciti failed to execute. You can use exit status in shell scripting too. You can store result of exit status in variable. Consider following shell script:

#!/bin/bash
echo -n "Enter user name : "
read USR
cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null
OUT=$?
if [ $OUT -eq 0 ];then
   echo "User account found!"
else
   echo "User account does not exists in /etc/passwd file!"
fi

Save and execute the script as follows:
$ chmod +x script.sh
$ ./script.sh

Output:

Enter user name : jradmin
User account does not exists in /etc/passwd file

Try it one more time:
$ ./script.sh
Output:

Enter user name : vivek
User account found

 

you can filter out messages to stderr. I prefer to redirect them to stdout like this.

find / -name art  2>&1 | grep -v “Permission denied”

Explanation:

In short, all regular output goes to standard output (stdout). All error messages to standard error (stderr).

grep usually finds/prints the specified string, the -v inverts this, so it finds/prints every string that doesn’t contain “Permission denied”. All of your output from the find command, including error messages usually sent to stderr (file descriptor 2) go now to stdout(file descriptor 1) and then get filtered by the grep command.

This assumes you are using the bash/sh shell.

Under tcsh/csh you would use

find / -name art |& grep ….

 

Shell:

jiafei427@CKUBU:~/workspace/kernel/modules/test$ cat infinity_touch.sh
i=0
while [ 1 ]
do
echo “wocao”
sleep 0.01
i=i+1
done

That will repeat print wocao with sleeping 0.01 second

…TBD

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s