Sunday, September 07, 2008

rename file extentions ...

I wanted to rename all .txt file extensions as .html

$rename s/\.html/.txt/ *.html

Oh, also, this isn't a "standard Unix(TM) command, but it does come with most Perl installations that I know of and Perl is on most Unix machines that I know of.

Also i tried for loop :

for i in *.txt; do mv "$i" `basename $i`.html

But this renames a file file1.txt as file1.txt.html

anyone know how get avoid .html added after .txt ?

Try this :

for i in "${i%.txt}"; do mv "$i" "${i%.txt}".html; done

Exit Status - Shell script

Whenever a shell script is executed it checks the exit status
of command to verify if it executed sucessfully.

True = 0
False = Non Zero Value

You can check exit status value as below:

Eg:

#!/bin/bash

echo hello
echo $?
# Exit status 0 returned because command executed successfully.

lskdf
Unrecognized command.
echo $? # Non-zero exit status returned because command failed to execute.

$./script.sh

hello
0

./script.sh line 5 lskdf command not found.
127

Shell script if statement

Shell script using if statement to check if process is running:
================================================================

#!/bin/bash

sendmail=`(ps -ef |grep -v grep |grep sendmail)`
if [ -z "$sendmail" ];

then
echo "Sendmail is not running"

else
echo "Sendmail is running"

fi


WHat each line means:
---------------------
1) #!/bin/bash
This generates a Process ID for shell

2) PROCESS=`(ps -ef |grep -v grep |grep sendmail)`
Defining Variable Process:
# When we grep a process ps -ef |grep sendmail, the output result also shows "grep sendmail" line, in order to avoid that we use grep -v grep.


3) if [ -z "$sendmail" ];

-z option tells it should return a null value(no value)

Check for more : man bash

-n str True if string str is not a null string
-z str True if string str is a null string