Programming
How execute a command in a separate directory without changing current working directory in Bash
Dec 6th
The following example shows how to move to a directory before executing a command and then automatically return back to the old diretory. This effectively runs the command with current directory as a different directory.
[neo@techpulp ~]# pwd /home/neo [neo@techpulp ~]# (cd /; ls) bin boot dev etc home lib lost+found media mnt opt proc root sbin selinux srv sys tmp usr var [neo@techpulp ~]# pwd /home/neo [neo@techpulp ~]#
As you can see, the commands are grouped in parenthesis so that the current directory remains same after running the commands.
How to list files by date in Linux
Dec 6th
The “ls -lt” command can be used to get the list of files sorted by their modification date.
[neo@techpulp ~]# ls -lt total 60 -rw-rw-r-- 1 neo neo 122 2008-12-06 01:39 monitor.sh -rw-rw-r-- 1 neo neo 44 2008-12-06 01:33 1.sh -rw-rw-r-- 1 neo neo 10381 2008-12-06 00:53 ifconfig.pdf drwxrwxr-x 3 neo neo 4096 2008-12-05 20:06 workspace drwxr-xr-x 3 neo neo 4096 2008-12-05 02:35 Desktop drwxrwxr-x 3 neo neo 4096 2008-12-05 01:46 Examples drwxr-xr-x 2 neo neo 4096 2008-12-05 00:59 Download drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Documents drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Music drwxr-xr-x 2 neo neo 4096 More >
How to get the PID of current bash shell script
Dec 6th
A special variable “$$” can be used with in a shell script to retrieve self process ID (PID). This is useful for monitoring scripts to store their PID in a file for other maintenance script to identify it. The following script shows how it can be done.
#!/bin/bash # store my PID echo $$ > /var/run/monitor.pid while [ 1 ]; do echo I am monitoring something sleep 5s done
Some scripts use their PID to create unique temporary files along with some fixed prefix. The following example script shows how it is done.
#!/bin/bash TMPFILE=myfile.$$ ls -l > $TMPFILE
However the above method serves More >
How to get exit code of previous command in Bash
Dec 5th
The special variable “$?” returns the exit code of last executed command in Bash shell script. The following example gets the exit code of “grep” command whose exit code will be zero if finds match and 1 otherwise.
#!/bin/bash grep neo users.txt if [ "$?" == "0" ]; then echo User neo detected else echo User neo not found fi
Why my application can’t open more than 1024 or 4096 sockets in Linux
Dec 5th
Typically socket “open” call doesn’t fail unless you have missed out closing the previous connections using “close” system call. Typically Linux/Unix sets a maximum limit for the number of open FDs. That means you can’t keep FDs in open state more than certain number. These settings can’t be changed as a normal user of the system.
These limits can be seen using “ulimit -a” command.
[neo@techpulp ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31729 max locked memory (kbytes, -l) 32 max memory More >
Bash shell script to convert string from lower to upper case and vice versa
Dec 5th
The “tr” command can be used to convert a string variable to upper or lower case. The following function in a bash script converts first argument to lower case.
Convert the given argument into an all lower case string.
toLower() {
echo $1 | tr "[:upper:]" "[:lower:]"
}
GENDER=MALE
GENDER=`toLower $GENDER`
echo $GENDER
Convert the given argument into an all upper case string.
toUpper() {
echo $1 | tr "[:lower:]" "[:upper:]"
}
GENDER=male
GENDER=`toUpper $GENDER`
echo $GENDER
Why GNU Make always says `target’ is up to date
Dec 5th
The GNU make expects the rule target to be files by default. That’s why it first checks the presense of a file/directory with the name specified in the rule. For example GNU make attempts to find if a file with name “target1″ with the following Makefile.
target1: prog prog: one.o two.o gcc $^ -o prog
If by chance you have a file or directory with name “target1” in the same directory, GNU Make interprets its last modified date. Because of that some times Make doesn’t compile anything saying target is up to date even if some of the dependencies are changed.
It is More >
GNU Make – How to compile all c files in present directory
Dec 5th
The “shell” built-in function supported by GNU Make can be used to get the list of C source files in a directory. Then a list of corresponding object files can be created by replacing the file extension in the file list. The following example shows how to compile all C source files present in the current directory to generate an executable named “target“. If you are doing copy and paste of the following example, ensure that you place TAB for all commands under each rule. Otherwise GNU Make complains about “missing separator” error.
TARGET=target SRCS=$(shell ls *.c) OBJS=$(SRCS:.c=.o) all: $(TARGET) $(TARGET):$(OBJS) More >
How to find file name of the script with in a bash shell script
Dec 5th
With in a bash shell script, you can use the very first command line argument ($0) to find the name of the script. The following example explains it.
[neo@techpulp ~]# cat myscript.sh #!/bin/bash echo The script name is $0 [neo@techpulp ~]# [neo@techpulp ~]# chmod +x myscript.sh [neo@techpulp ~]# [neo@techpulp ~]# ./myscript.sh The script name is ./myscript.sh [neo@techpulp ~]#
Where my bash command history is stored
Nov 21st
The bash shell stores all your command history in a file of your home directory. Typically it will be stored in ~/.bash_history. This is a global history of all bash terminals which are open. The bash shell appends all commands executed in a terminal just before closing the terminal.
The history command can be used to view the active history at bash shell prompt.
[neo@techpulp ~]# history 1 ls 2 vi test.c 3 gcc test.c -o test 4 ./test 5 pwd 6 history [neo@techpulp ~]#
The number followed by NOT symbol (!) can be used to repeat a specific command. The following example shows how to More >


Recent Comments