ittechlab
asked on
script
I have a requirement to read each line in a file and process that line.
Can some one help me with this requirement. For example i want to read /etc/passwd file line by line and stored in a array. How would I do that. I am able to read the line but i want to know how to stored each line in array and get it later.
#!/bin/ksh
file="/etc/passwd"
while read line
do
done <"$file"
Can some one help me with this requirement. For example i want to read /etc/passwd file line by line and stored in a array. How would I do that. I am able to read the line but i want to know how to stored each line in array and get it later.
#!/bin/ksh
file="/etc/passwd"
while read line
do
done <"$file"
Not sure what your future intent is but this site may help and gives many different options: http://stackoverflow.com/questions/4920995/how-to-store-etc-passwd-in-a-hash-or-array
#!/bin/ksh
file="/etc/passwd"
i=0
while read line
do
a[$i]="$line"
(( i = i + 1 ))
done < $file
ASKER
can i able to use for loop ? is the logic wrong?
I tried this and didn't work.
#!/bin/bash
printf "Enter full path of a file name \c:"
read fileNamePath
counter=0
#File=$(echo $fileNamePath)
for line in `cat $fileNamePath`
do
FileArray[$counter]=$(echo $line)
echo ${FileArray[$counter]}
(( counter++ ))
done
printf "The full path of the file is $fileNamePath\n"
I tried this and didn't work.
#!/bin/bash
printf "Enter full path of a file name \c:"
read fileNamePath
counter=0
#File=$(echo $fileNamePath)
for line in `cat $fileNamePath`
do
FileArray[$counter]=$(echo
echo ${FileArray[$counter]}
(( counter++ ))
done
printf "The full path of the file is $fileNamePath\n"
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
what is word splitting? can you give a example please.
ASKER
I tried the following. it works. Please explain where will be the word split happens. I am trying to understand the issue as i am new to the script.
#!/bin/bash
printf "Enter full path of a file name \c:"
read fileNamePath
counter=0
#File=$(echo $fileNamePath)
for line in `cat $fileNamePath`
do
FileArray[$counter]=$line
#echo ${FileArray[$counter]}
(( counter++ ))
done
printf "The full path of the file is $fileNamePath\n"
printf "The full path of the file is ${FileArray[4]}\n"
#!/bin/bash
printf "Enter full path of a file name \c:"
read fileNamePath
counter=0
#File=$(echo $fileNamePath)
for line in `cat $fileNamePath`
do
FileArray[$counter]=$line
#echo ${FileArray[$counter]}
(( counter++ ))
done
printf "The full path of the file is $fileNamePath\n"
printf "The full path of the file is ${FileArray[4]}\n"
It works when you don't have spaces in the file names.
Read this:
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/WordSplitting
Read this:
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/WordSplitting
My question would be why do you want to use an array?
ASKER
Thanks Farzanj
please assist with the following. when i choose unmount option and type /n its shows two mount /nas and /nat. i want to be able to go by exact word. lets say some one type /n it should complain there is no mount /n exists and if they type /nat it will ask for confirmation.
#!/bin/ksh
echo "Menu for Mount/unmount"
echo "----------------------"
echo "a. Mount status "
echo "b. unmount"
echo "c. Mount"
echo "d. exit"
echo " "
function mount_status
{
echo "Please Enter mount point"
read mPoint
mount | grep $mPoint
if [ $? -eq 0 ]
then
echo "$mPoint currently mounted"
else
echo "$mPoint currently not mounted"
fi
}
function unmount
{
echo "Please Enter the mount point which you wanted to unmount"
read mPoint
mount | grep $mPoint
echo "Are you sure you want to unmount $mPoint. yes or no"
read answer
if [ $answer = "yes" ]
then
echo "I think you are right"
elif [ $answer = "no" ]
then
echo "Make up your mind"
else
echo "learn how to type yes or no. Bye for now"
fi
}
read answer
case $answer in
a) mount_status;;
b) unmount;;
c) echo You chose c;;
d) exit 1;;
*) echo You must choose either a,b,c or d
esac
please assist with the following. when i choose unmount option and type /n its shows two mount /nas and /nat. i want to be able to go by exact word. lets say some one type /n it should complain there is no mount /n exists and if they type /nat it will ask for confirmation.
#!/bin/ksh
echo "Menu for Mount/unmount"
echo "----------------------"
echo "a. Mount status "
echo "b. unmount"
echo "c. Mount"
echo "d. exit"
echo " "
function mount_status
{
echo "Please Enter mount point"
read mPoint
mount | grep $mPoint
if [ $? -eq 0 ]
then
echo "$mPoint currently mounted"
else
echo "$mPoint currently not mounted"
fi
}
function unmount
{
echo "Please Enter the mount point which you wanted to unmount"
read mPoint
mount | grep $mPoint
echo "Are you sure you want to unmount $mPoint. yes or no"
read answer
if [ $answer = "yes" ]
then
echo "I think you are right"
elif [ $answer = "no" ]
then
echo "Make up your mind"
else
echo "learn how to type yes or no. Bye for now"
fi
}
read answer
case $answer in
a) mount_status;;
b) unmount;;
c) echo You chose c;;
d) exit 1;;
*) echo You must choose either a,b,c or d
esac