Unknown Problem. I really want your help to solve this!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unknown Problem. I really want your help to solve this!
# 1  
Old 05-11-2010
PHP Unknown Problem. I really want your help to solve this!

Take a look on this code:
Code:
#!/bin/sh
currentpath=`pwd`
if [ `id -u` != 0 ]; then
#Normal user
if [ -s "$currentpath"/$USER ]; then
"$currentpath"/.cleaner
else
./runit
fi
else
#Root user
if [ -s /some ]; then
rm -r /some
fi
mkdir /some
cd /home/
echo "`ls --group-directories-first -1`" > /some/allusers
cat /some/allusers | sed 's/ /\\ /g' > /some/allrealusers
user1=`cat /some/allrealusers | awk 'NR>0 && NR<2{print $0}'`
user2=`cat /some/allrealusers | awk 'NR>1 && NR<3{print $0}'`
user3=`cat /some/allrealusers | awk 'NR>2 && NR<4{print $0}'`
user4=`cat /some/allrealusers | awk 'NR>3 && NR<5{print $0}'`
user5=`cat /some/allrealusers | awk 'NR>4 && NR<6{print $0}'`
user6=`cat /some/allrealusers | awk 'NR>5 && NR<7{print $0}'`
user7=`cat /some/allrealusers | awk 'NR>6 && NR<8{print $0}'`
user8=`cat /some/allrealusers | awk 'NR>7 && NR<9{print $0}'`
user9=`cat /some/allrealusers | awk 'NR>8 && NR<10{print $0}'`
rm -r /some
if [ -s "$currentpath"/$user1 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user2 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user3 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user4 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user5 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user6 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user7 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user8 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user9 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s /some ]; then
rm -r /some
fi
clear
"$currentpath"/.runit
fi

Image Problem in Unix Script (maybe if-else.....)
Take a look on this code:
Code:
#!/bin/sh
currentpath=`pwd`
if [ `id -u` != 0 ]; then
#Normal user
if [ -s "$currentpath"/$USER ]; then
"$currentpath"/.cleaner
else
./runit
fi
else
#Root user
if [ -s /some ]; then
rm -r /some
fi
mkdir /some
cd /home/
echo "`ls --group-directories-first -1`" > /some/allusers
cat /some/allusers | sed 's/ /\\ /g' > /some/allrealusers
user1=`cat /some/allrealusers | awk 'NR>0 && NR<2{print $0}'`
user2=`cat /some/allrealusers | awk 'NR>1 && NR<3{print $0}'`
user3=`cat /some/allrealusers | awk 'NR>2 && NR<4{print $0}'`
user4=`cat /some/allrealusers | awk 'NR>3 && NR<5{print $0}'`
user5=`cat /some/allrealusers | awk 'NR>4 && NR<6{print $0}'`
user6=`cat /some/allrealusers | awk 'NR>5 && NR<7{print $0}'`
user7=`cat /some/allrealusers | awk 'NR>6 && NR<8{print $0}'`
user8=`cat /some/allrealusers | awk 'NR>7 && NR<9{print $0}'`
user9=`cat /some/allrealusers | awk 'NR>8 && NR<10{print $0}'`
rm -r /some
if [ -s "$currentpath"/$user1 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user2 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user3 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user4 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user5 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user6 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user7 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user8 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s "$currentpath"/$user9 ]; then
"$currentpath"/.cleaner
exit
fi
if [ -s /some ]; then
rm -r /some
fi
clear
"$currentpath"/.runit
fi

Well, the program first checks if the user who run it is root or not. The problem is when the program is ran by root. Then, a file is created which contains the names of each user, and creates some variables user1, user2 etc with the usernames. Well, if the program is run by root, even if in the current path there isn't any folder or file with the name of any user, it runs the ./cleaner and not the ./runit as it should be. I cannot understand my fault and I am trying to solve this problem some hours now...

Last edited by Scott; 05-11-2010 at 01:45 PM.. Reason: More code tags
# 2  
Old 05-11-2010
That's a very odd way to code the program, running awk 9 times to process one flat file... You've also got many useless uses of cat in there. That's going to be extremely slow.

You can probably do that in a loop, without cat or awk, and perhaps fix the problem at the same time, what does realusers look like?
# 3  
Old 05-11-2010
$USER is not set anywhere.
# 4  
Old 05-11-2010
And regarding your problem, I think the easiest way is to put echo above the if condition.


Code:
echo "$currentpath"/$USER

if [ -s "$currentpath"/$USER ]; then

 echo "inside if"
 #"$currentpath"/.cleaner
else
 echo "inside if"
 #./runit
fi

# 5  
Old 05-11-2010
Also, just because a directory's empty doesn't mean it has a size of zero. So the -s flag probably isn't doing what you think.

In short, your program's got deeper problems than a minor glitch. Can you tell us exactly what it's supposed to do?

Last edited by Corona688; 05-11-2010 at 02:00 PM..
# 6  
Old 05-11-2010
Quote:
Originally Posted by methyl
$USER is not set anywhere.

$USER is a system variable or something?
Correct me if wrong.
# 7  
Old 05-11-2010
Quote:
Originally Posted by anchal_khare
$USER is a system variable or something?
Correct me if wrong.
It is here, at least.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me solve this scripting problem please

Hello, I would really appreciate some help into approaching this problem: - i have a random txt file with x lines and y rows following this pattern: ex: ip1 | user1 | command ip2 | user2 | command ip3 | user3 | command - i need to telnet/ssh into these ip's, login with... (7 Replies)
Discussion started by: catalinstk
7 Replies

2. Shell Programming and Scripting

How to solve the problem of overwriting an array?

Hi all, I have a file..... I want to print 2nd column arranged according to order of first column, present in second file..... So, the output should be: I am using following code: awk 'NR==FNR{a=$2;next}{print a?a:"ABSENT\t"}' file1 file2 But, it seems that the... (3 Replies)
Discussion started by: CAch
3 Replies

3. UNIX for Advanced & Expert Users

how would you solve this problem?

I have a file process.txt I wanted to just grab data in "process" column. Name process process_id status Adminserver adminserver 22669 Running Browser Engine browserengine ... (7 Replies)
Discussion started by: soemac
7 Replies

4. IP Networking

Need to solve complex network problem

I have a Red Hat linux server X on a x.x.0.0 network. This machine also has to communicate with another server Y on a network called y.y.0.0 Server X has two network interfaces. eth0 is configured on the x.x.0.0 network and has a default gateway on the x.x.0.0 network. In order to... (4 Replies)
Discussion started by: soliberus
4 Replies

5. UNIX for Advanced & Expert Users

How to solve restarting problem

Hi! My unix os version is OSF1 CP1 V4.0 878 alpha. It startup normally but it restarts within 5 sec. I would like to know how to solve . Please reply to me. Thanks . akzin (2 Replies)
Discussion started by: akzin
2 Replies

6. UNIX for Advanced & Expert Users

use two unix commands to solve the following problem

Hi, all, The following commands could compute the 10 most frequent bigrams from a input sequence which is in a file infile. I would like to know whether there is somebody who can use only two unix commands to do the same work. -------------------- tr " " "\012*" <infile >out1 tail +2... (3 Replies)
Discussion started by: vicky20000
3 Replies

7. Programming

Can any one solve this Problem...!!!

Try to solve this.....It's a nice program..... #include<stdio.h> void change() { /*Write something in this function so that the output of printf in main function should give 5 . Do not change the main function */ } void main() { int i=5; change(); (9 Replies)
Discussion started by: Baba B. Saheb
9 Replies

8. UNIX for Advanced & Expert Users

can't solve that problem [PLEASE HELP]

well, my internet brakes down every day because of my server, i don't have troubles with RAM or anything i think... that problem started since i am running an unrealircd server... well, my internet brakes down and when i try to access the inside ip from the server on http port 80, it says that:... (2 Replies)
Discussion started by: AiRkO
2 Replies

9. Programming

How can I solve this problem?

I'm now designing a server application which can serve large number of clients' request. I've a question to ask, that is, main process will block when invoke the "accept" function, if a client request comes, main process should be separated into 2 processes by invoking "fork" function, the parent... (4 Replies)
Discussion started by: acqy
4 Replies
Login or Register to Ask a Question