LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2009, 05:58 PM   #1
john smith
LQ Newbie
 
Registered: Mar 2009
Posts: 14

Rep: Reputation: 0
problem with shell script


hi i am new to linux when i tried to run a shell script i got am error which is
[root@localhost bin]# ./crtinv.sh
bash: ./crtinv.sh: /bin/sh^M: bad interpreter: No such file or directory
where crtinv.sh is the name of the file
 
Old 03-14-2009, 06:10 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Hi and welcome to LQ!
Code:
^M
Most likely the script has been written in a DOS (Windows) environment, so that it has retained the line terminator made by a carriage return plus a newline control character. The line feed in *nix environments is made by a single newline character. The *nix shell cannot interpret that sequence and spit out an error. To convert the file from dos to unix format, just use the command
Code:
dos2unix crtinv.sh
Just a note: in *nix the DOS line feed often is represented by the sequence \r\n. If you want to type a ^M in your terminal, just press Ctrl-V and Ctrl-M in sequence.

A clearly explained reference about "newline" is the Wikipedia page. It explains other methods in *nix to make the conversion.

Last edited by colucix; 03-14-2009 at 06:15 PM. Reason: Added reference
 
Old 03-14-2009, 06:10 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
you edited the file in notepad (or similar) under windows didn't you? run "dos2unix crtinv.sh" and try again. ensure you have dos2unix installed first of course.
 
Old 03-14-2009, 06:35 PM   #4
john smith
LQ Newbie
 
Registered: Mar 2009
Posts: 14

Original Poster
Rep: Reputation: 0
thanx both of u
 
Old 03-14-2009, 07:03 PM   #5
john smith
LQ Newbie
 
Registered: Mar 2009
Posts: 14

Original Poster
Rep: Reputation: 0
this is the code which i actually tried to run but it gives me error
[root@localhost project]# ./crtinv.sh a
\n0 valid explorer found in...: /e/project\n
\n
the code is
[root@localhost project]# ./crtinv.sh a
\n0 valid explorer found in...: /e/project\n
\n
#!/bin/bash
#*********************************************************
#Script to create inventory from Explorer Dump
#*********************************************************

#*********Default values of parameters********************

#Path of the Explorer Dump.The default is Current directory.
ExpPath="`pwd`"

#Path of the Report Directory.The default is the report
#directory in the home of the user.
#Set the Report Directory and the Default directory
RptPath="$HOME/report"

#Number of valid explorer found in the explorer directory
#The default is Zero.
ExpNum=0

#Set the information bit to 0 (Zero) for default value
#If the value is set to 1 then the information is searched
#to all the explorer
disk_bit=0
hostname_bit=0
hostid_bit=0
kerver_bit=0
systyp_bit=0
memory_bit=0
ip_bit=0
cpu_bit=0
expdate_bit=0
expver_bit=0

#Get the current date
#Date=`date|awk -F' ' '{print $4$2$3":"$6}'`
Date="`date '+%S_%M_%H_%d_%h_%Y'`"

#*************Function deffination bellow****************
get_cpu()
{

#Function to get the CPU information
sed -n '/CPUs/,/^=\{10\}/p' $1 | sed -e '/^=\{10\}/d' -e '/^$/d'
}

get_disk()
{

#Function to get the disk information
sed '/AVAILABLE/d
/^$/d' $1
}

get_hostname()
{

#Function to get the hostname infromation
sed -n '/Hostname/p' $1 |cut -d' ' -f2
}

get_hostid()
{

#Function to get the hostid information
sed -n '/Hostid/p' $1 |cut -d' ' -f2
}

get_kerver()
{

#Function to get the kernel version information
sed -n '/Kernel version/p' $1 |cut -d':' -f2
}

get_systyp()
{

#Function to get the system type information
sed -n '/System Configuration/p' $1 |cut -d':' -f2
}

get_memory()
{

#Function to get RAM memory information
sed -n '/Memory size/p' $1 |cut -d':' -f2
}

get_ipaddr()
{

#Function to get the IP address information
sed -e '/Name/d' -e '/lo/d' -e '/^$/d' $1 |awk -F' ' '{print $1,":",$4}'
}

get_expver()
{

#Function to get the Eplorer Version information
awk '/Version/' $1 |cut -d'(' -f3 |cut -d')' -f1
}

get_expdate()
{

#Function to get the Explorer Date & Time information
sed -n '/Date/p' $1 |cut -d' ' -f2
}

validate_exppath()
{

#This function validates the existence of
#explorer path provided by the user and the
#checks the required permissions

path="`pwd`"
if [ -d $ExpPath ]
then
cd $ExpPath
for dir in *
do
if [ -d $dir ]
then
if [ -f $dir/README -a -f $dir/sysconfig/prtdiag-v.out -a -f $dir/disks/diskinfo ]
then
ExpNum=`expr $ExpNum + 1`
else
echo "$dir is not a valid explorer."
fi
fi
done
echo "\n$ExpNum valid explorer found in...: $ExpPath\n"
if [ $ExpNum -eq 0 ]
then
echo "\n"
exit 2
fi
else
echo "Invalid explorer path."
exit 2
fi
cd $path
}

validate_rptpath()
{

#This function checks for the existence of
#the report forlder and it checks for the
#required permissions and if the report folder
#does not exists then creates it.

if [ -d $RptPath ]
then
if [ ! -w $RptPath ]
then
echo "Write permission required to the report folder $RptPath."
exit 3
else
echo "Report directory is...: $RptPath"
fi
else
echo "Creating report directory....: $RptPath"
mkdir -p $RptPath
fi
}

create_report()
{

#This function travels all the explorers and collects
#all the Information whose bit field is set to 1

#Save the current path
path="`pwd`"

#Go to the explorer folder
cd $ExpPath

#Creating Report
echo "Creating Report.\c"

#Goto each explorer folder and create the intermidiate report
for dir in *
do
paste_string=""
if [ -d $dir ]
then

#Get the Hostname information and put it in the hostname.$$.tmp file
#if the hostname_bit is set to 1
if [ $hostname_bit -eq 1 ]
then
get_hostname $dir/README > $RptPath/hostname.$$.tmp
paste_string=$paste_string" hostname.$$.tmp"
fi

#Get the HostID information and put it in the hostid.$$.tmp file
#if the hostid_bit is set to 1
if [ $hostid_bit -eq 1 ]
then
get_hostid $dir/README > $RptPath/hostid.$$.tmp
paste_string=$paste_string" hostid.$$.tmp"
fi

#Get the Kernel Version information and put it in the kerver.$$.tmp file
#if the kerver_bit is set to 1
if [ $kerver_bit -eq 1 ]
then
get_kerver $dir/README > $RptPath/kerver.$$.tmp
paste_string=$paste_string" kerver.$$.tmp"
fi

#Get the System Type information and put it in the systyp.$$.tmp file
#if the systyp_bit is set to 1
if [ $systyp_bit -eq 1 ]
then
get_systyp $dir/sysconfig/prtdiag-v.out > $RptPath/systyp.$$.tmp
paste_string=$paste_string" systyp.$$.tmp"
fi

#Get the Memory information and put it in the memory.$$.tmp file
#if the memory_bit is set to 1
if [ $memory_bit -eq 1 ]
then
get_memory $dir/sysconfig/prtdiag-v.out > $RptPath/memory.$$.tmp
paste_string=$paste_string" memory.$$.tmp"
fi

#Get the network device and IP address information and put them in the ip.$$.tmp file
#if the ip_bit is set to 1
if [ $ip_bit -eq 1 ]
then
get_ipaddr $dir/netinfo/netstat-in.out > $RptPath/ip.$$.tmp
paste_string=$paste_string" ip.$$.tmp"
fi

#Get the disk information and put them in the disk.$$.tmp file
#if the disk_bit is set to 1
if [ $disk_bit -eq 1 ]
then
get_disk $dir/disks/diskinfo > $RptPath/disk.$$.tmp
paste_string=$paste_string" disk.$$.tmp"
fi

#Get the CPU information and put them in the cpu.$$.tmp file
#if the cpu_bit is set to 1
if [ $cpu_bit -eq 1 ]
then
get_cpu $dir/sysconfig/prtdiag-v.out > $RptPath/cpu.$$.tmp
paste_string=$paste_string" cpu.$$.tmp"
fi

#Get the Explorer Version information and put them in the expver.$$.tmp file
#if the expver_bit is set to 1
if [ $expver_bit -eq 1 ]
then
get_expver $dir/README > $RptPath/expver.$$.tmp
paste_string=$paste_string" expver.$$.tmp"
fi

#Get the Explorer Date information and put them in the expdate.$$.tmp file
#if the expdate_bit is set to 1
if [ $expdate_bit -eq 1 ]
then
get_expdate $dir/README > $RptPath/expdate.$$.tmp
paste_string=$paste_string" expdate.$$.tmp"
fi

cd $RptPath
paste -d~ $paste_string >> report.$Date.rpt

cd $ExpPath
echo ".\c"
fi
done
cd $RptPath
rm *.$$.tmp > /dev/null 2>&1
cd $path
echo "\n"
}

show_help()
{

#This function shows the help page

echo "
Usage:

`basename $0` -h

`basename $0` -a [-E explorer_path] [-R report_path]

`basename $0` [-n ] [-H ] [-k ] [-s ] [-r ] [-i ] [-d ] [-c ] \\
[-t ] [-v ] [-E explorer_path] [-R report_path]

Where:
-h Show this help on syntax and usage. Other options will be ignored.
-a Includes all the information as Hostname,Hostid,Kernel Version,System Type
or Model,RAM Memory,IP Address,Disk,CPU,Explorer Version and Explorer Date.
-n Includes Hostname information in the report.
-H Includes HostID information in the report.
-k Includes Kernel Version information in the report.
-s Includes System Type or Model information in the report.
-r Includes RAM Memory information in th ereport.
-i Includes IP Address information in the report.
-d Includes Disk information in the report.
-c Includes CPU information in the report.
-v Includes Explorer Version information in the report.
-t Includes Date & Time of the Explorer taken in the report.
-E expr_path Specify the path of the Explorer Dumps.Default is the
current directory of the command.
-R rpt_path Specify the path of the Report to be created.Default is the $HOME/report
directory of the current user.
"
}

# Main program body begins here

#Exit if no option is there in the command line
#show the help page
if [ $# -eq 0 ]
then
show_help
exit 1
fi

# Analyze the options and arguments

while getopts hanHksridcvtE:R: var; do
case $var in
h)
show_help
exit 0;;
'?')
show_help
exit 1;;
':')
show_help
exit 1;;
a)
hostname_bit=1
hostid_bit=1
kerver_bit=1
systyp_bit=1
memory_bit=1
ip_bit=1
disk_bit=1
cpu_bit=1
expver_bit=1
expdate_bit=1;;
n)
hostname_bit=1;;
H)
hostid_bit=1;;
k)
kerver_bit=1;;
s)
systyp_bit=1;;
r)
memory_bit=1;;
i)
ip_bit=1;;
d)
disk_bit=1;;
c)
cpu_bit=1;;
v)
expver_bit=1;;
t)
expdate_bit=1;;
E)
ExpPath=$OPTARG;;
R)
RptPath=$OPTARG;;
*)
echo "\nError parsing command line"
show_help
exit 1;;
esac;
done

validate_exppath
validate_rptpath
create_report
exit 0

i have got this code from internet so could any one tell me what the explorer dump means

Last edited by john smith; 03-14-2009 at 07:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script problem Abstractt Linux - Software 4 09-16-2008 05:00 PM
shell script problem bondoq Linux - General 3 07-03-2006 07:33 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Problem in shell script Kumar Programming 4 04-27-2004 09:48 AM


All times are GMT -5. The time now is 03:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration