shell scripting week 4
1) File-related commands: Test the function of each of the following.
a) File permissions: –r, –w, –x
1) File-related commands: Test the function of each of the following.
a) File permissions: –r, –w, –x#!\bin\bashecho "enter the file name"read fileif [ -r $file -a -w $file -a -x $file ] thenecho "file is readable ,writable and excutable"elif [ -w $file -a -r $file ]thenecho "file is writeable and readable"elif [ -x $file -a -w $file ]thenecho "file is excutable and writable"elif [ -r $file -a -x $file ]thenecho "file is readable and excutable"elif [ -r $file ] thenecho "file is readable"elif [ -w $file ]thenecho "file is writeable"elif [ -x $file ]thenecho "file is excutable"elseecho "entered file does not exist"fi
b) File existence: –f, –d, –e, –s
#!\bin\bashecho "enter the name to check file directory exixtence and non zero file"read file
if [ -f $file -a -e $file -a -s $file ]thenecho "entered name is file ,non zero in size and existing"elif [ -e $file -a -d $file ]thenecho "entered name is existing and a directory"elif [ -d $file -a -s $file ]thenecho "entered name is directory and non zero in size"elif [ -s $file -a -f $file ] thenecho "file is non zero in size"elif [ -f $file ]thenecho "entered name is file"elif [ -e $file ]thenecho "file is existing"elif [ -d $file ]thenecho "entered name is directory"elif [ -s $file ] thenecho "file or directory is non zero in size"fi2) Write and execute shell scripts for the following (Using file related commands):
c) List all files in the directory which have a read permission.
#!\bin\bashecho "enter 1 to check file in home directory enter 2 to check in present directory"read chooseif [ $choose -eq 1 ]thencd ~echo "enter directory name to list files to find it is readable"read filescd $filesecho "these are readables files"for file in * do if [ -f $file ]then if [ -r $file ] then echo "$file" fifidonefi
if [ $choose -eq 2 ]thenecho "your present directory is"pwdecho "these are readable files"for file in * do if [ -f $file ]then if [ -r $file ] then echo "$file" fifidonefi
d) Input a file name. Output whether it has read, write and execute permissions or not.
#!/bin/bashecho "enter the file name to check it has read write exicute permission"read file
if [ -r $file -a -w $file -a -x $file ] thenecho "file is readable ,writable and excutable"elif [ -w $file -a -r $file ]thenecho "file is writeable and readable"elif [ -x $file -a -w $file ]thenecho "file is excutable and writable"elif [ -r $file -a -x $file ]thenecho "file is readable and excutable"elif [ -r $file ] thenecho "file is readable"elif [ -w $file ]thenecho "file is writeable"elif [ -x $file ]thenecho "file is excutable"elseecho "file has no read write and excutable permission"fi
e) Accept a file name. Check whether it has write permission. If yes, append some new text to it, otherwise display an error message.
#!/bin/bashecho "enter a file name"read filetouch $filels -l $fileif [ -w $file ]then echo "hello there is your appended text" >> $filecat $fileelseecho "file does not have write permision"fi 3) Write and execute shell scripts for the following (Using file related commands):
f) Count and display the number of ordinary files, hidden files and sub- directories present in the working directory.
#!/bin/bashecho "these are the odinary files"ordinary=0for file in *doif [ -f $file ]then# echo "$file"to display file nameordinary=`expr $ordinary + 1`fidoneecho "$ordinary"echo "these are the directory"directory=0for file in *doif [ -d $file ]thendirectory=`expr $directory + 1`fidone echo "$directory"
all=0for file in `ls -a`doif [ -f $file ]thenall=`expr $all + 1`fidoneecho "these are the hidden files"hidden=` expr $all - $ordinary`echo "$hidden"
g) Display the names of all files in the working directory which have size greater than 0.
#!\bin\bash
echo "enter the file name"
read file
if [ -r $file -a -w $file -a -x $file ]
then
echo "file is readable ,writable and excutable"
elif [ -w $file -a -r $file ]
then
echo "file is writeable and readable"
elif [ -x $file -a -w $file ]
then
echo "file is excutable and writable"
elif [ -r $file -a -x $file ]
then
echo "file is readable and excutable"
elif [ -r $file ]
then
echo "file is readable"
elif [ -w $file ]
then
echo "file is writeable"
elif [ -x $file ]
then
echo "file is excutable"
else
echo "entered file does not exist"
fi
b) File existence: –f, –d, –e, –s
#!\bin\bash
echo "enter the name to check file directory exixtence and non zero file"
read file
if [ -f $file -a -e $file -a -s $file ]
then
echo "entered name is file ,non zero in size and existing"
elif [ -e $file -a -d $file ]
then
echo "entered name is existing and a directory"
elif [ -d $file -a -s $file ]
then
echo "entered name is directory and non zero in size"
elif [ -s $file -a -f $file ]
then
echo "file is non zero in size"
elif [ -f $file ]
then
echo "entered name is file"
elif [ -e $file ]
then
echo "file is existing"
elif [ -d $file ]
then
echo "entered name is directory"
elif [ -s $file ]
then
echo "file or directory is non zero in size"
fi
2) Write and execute shell scripts for the following (Using file related commands):
c) List all files in the directory which have a read permission.
#!\bin\bash
echo "enter 1 to check file in home directory enter 2 to check in present directory"
read choose
if [ $choose -eq 1 ]
then
cd ~
echo "enter directory name to list files to find it is readable"
read files
cd $files
echo "these are readables files"
for file in *
do
if [ -f $file ]
then
if [ -r $file ]
then
echo "$file"
fi
fi
done
fi
if [ $choose -eq 2 ]
then
echo "your present directory is"
pwd
echo "these are readable files"
for file in *
do
if [ -f $file ]
then
if [ -r $file ]
then
echo "$file"
fi
fi
done
fi
d) Input a file name. Output whether it has read, write and execute permissions or not.
#!/bin/bash
echo "enter the file name to check it has read write exicute permission"
read file
if [ -r $file -a -w $file -a -x $file ]
then
echo "file is readable ,writable and excutable"
elif [ -w $file -a -r $file ]
then
echo "file is writeable and readable"
elif [ -x $file -a -w $file ]
then
echo "file is excutable and writable"
elif [ -r $file -a -x $file ]
then
echo "file is readable and excutable"
elif [ -r $file ]
then
echo "file is readable"
elif [ -w $file ]
then
echo "file is writeable"
elif [ -x $file ]
then
echo "file is excutable"
else
echo "file has no read write and excutable permission"
fi
e) Accept a file name. Check whether it has write permission. If yes, append some new text to it, otherwise display an error message.
#!/bin/bash
echo "enter a file name"
read file
touch $file
ls -l $file
if [ -w $file ]
then
echo "hello there is your appended text" >> $file
cat $file
else
echo "file does not have write permision"
fi
3) Write and execute shell scripts for the following (Using file related commands):
f) Count and display the number of ordinary files, hidden files and sub- directories present in the working directory.
#!/bin/bash
echo "these are the odinary files"
ordinary=0
for file in *
do
if [ -f $file ]
then
# echo "$file"to display file name
ordinary=`expr $ordinary + 1`
fi
done
echo "$ordinary"
echo "these are the directory"
directory=0
for file in *
do
if [ -d $file ]
then
directory=`expr $directory + 1`
fi
done
echo "$directory"
all=0
for file in `ls -a`
do
if [ -f $file ]
then
all=`expr $all + 1`
fi
done
echo "these are the hidden files"
hidden=` expr $all - $ordinary`
echo "$hidden"
#!/bin/bash
echo "these are the files whose size is greater than zero"
for file in *
do
if [ -f $file ]
then
if [ -s $file ]
then
echo $file
fi
fi
done
#include
ReplyDeleteusing namespace std;
class crickter
{
public:
char player_name[25];
int total_matches_played,player_code, total_runs_all_matchs, number_of_not_outs, number_of_matches_played;
float avg_of_singl_play;
int count = 0;
void read_player_details()
{
cout << "\nEnter PlayerName,PlayerCode,Total Runs ,Number of Not Outs, and Number of Matches Played By Him : ";
cin >> player_name >> player_code >> total_runs_all_matchs >> number_of_not_outs>>number_of_matches_played;
}
void display_avg(int plcode)
{
if (player_code == plcode)
{
avg_of_singl_play = total_runs_all_matchs / float(number_of_matches_played - number_of_not_outs);
cout << "\n" << player_name << "\t\t\t" << total_runs_all_matchs << "\t\t\t" << avg_of_singl_play;
}
}
int display_avg(int number,crickter c)
{
avg_of_singl_play = 0;
if (count <= number)
{
avg_of_singl_play += c.total_runs_all_matchs / float(c.number_of_matches_played - c.number_of_not_outs);
count++;
return avg_of_singl_play;
}
}
};
void display_sorted_list_accto_total_runs(int number,crickter c[10])
{
for (int i = 0; i < number-1; i++)
{
crickter temp;
if (c[i].total_runs_all_matchs > c[i + 1].total_runs_all_matchs)
{
temp = c[i];
c[i] = c[i + 1];
c[i + 1] = temp;
}
}
for (int i = 0; i < number; i++)
{
cout << "\n" << c[i].total_runs_all_matchs;
}
}
int main()
{
crickter c[10];
int choice, number,play_code;
float avg=0;
do
{
cout << "\n1.Enter Player Details";
cout << "\n2.Display Average Runs of a Single Player";
cout << "\n3.Display Average of all Players";
cout << "\n4.Display List of Players in a Sorted Order";
cout << "\n5.Exit";
cout << "\n Please Select AnyOne Option From Above : ";
cin >> choice;
switch (choice)
{
case 1:cout << "\nFor How many Players You Want To Enter Details : ";
cin >> number;
for (int i = 0; i < number; i++)
{
c[i].read_player_details();
}
break;
case 2:
cout << "Enter a Player Code For Which You Want To Display Average Runs : ";
cin>>play_code;
cout << "\nPlayerName\t\tTotalRunsScored\t\tAverageRuns";
for (int i = 0; i < number; i++)
{
c[i].display_avg(play_code);
}
break;
case 3:
for (int i = 0; i < number; i++)
{
avg+=c[i].display_avg(number,c[i]);
}
avg /= number;
cout << "Average of All Team is : " << avg;
break;
case 4:display_sorted_list_accto_total_runs(number,c);
break;
default:break;
}
} while (choice != 5);
}
1 //name satyam tiwari
ReplyDelete2 #include
3 using namespace std;
4 class criket
5 {
6 public:
7 char p_name[20];
8 int total_matches_played,player_code,total_runs,num_notout,num_match_play;
9 float avg_singlplay;
10 int count=0;
11 void read_data()
12 {
13 cout<<"\n enter player name ,player code, total runs,no of not out,number of matches played:";
14 cin>>p_name>>player_code>>total_runs>>num_notout>>num_match_play;
15 }
16 void display_data(int pcode)
17 {
18 if(player_code==pcode)
19 {
20 avg_singlplay=total_runs/float(num_match_play-num_notout);
21 cout<<"\n"<a[i+1].total_runs)
45 {
46 cri=a[i];
47 a[i]=a[i+1];
48 a[i+1]=cri;
49 }
50 }
51 for(int i=0;i>choose;
70 if (choose=1)
71 {
72 cout<<"enter the number of players to fill data";
73 cin>>number;
74 for(int i=0;i>p_a_code;
84 cout<<"\n playername\t\t total runs\t\t average runs";
85 for(int i=0;i<number;i++)
86 {
87 a[i].display_avg(p_a_code);
88 }
89 }
90 else if(choose=3)
91 {
92 for(int i=0;i<number;i++)
93 {
94 avg+=a[i].display_avg(number,a[i]);
95 }
96 avg/=number;
97 cout<<"average of all team is"<<avg;
98 }
99 else if(choose=4)
100 {
101 dis_total_runs(number,a);
102 }
103 }while(choose!=5);
104 }