Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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.
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 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]
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.