LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-15-2004, 07:09 PM   #1
king_nothingzzz
Member
 
Registered: Aug 2003
Location: Brisbane, Australia
Distribution: Red Hat Enterprise Linux 4
Posts: 120

Rep: Reputation: 15
Angry Simple script problems...


Hi

I was trying to write a simple script really, just to see if the user is root or not. If root -> then execute the next steps (mount windows partition), else exit..

The script i wrote is as follows

Code:
#!/bin/bash
#####Check for UID =  0

clear
ID='id | awk '{print $1;}'|grep "uid=0"'
if [ "$ID" = "uid=0(root)" ]
then
echo; echo "ID is root. [$ID] Continuing ...";echo
else
echo; echo "This script must run as root. Your UID is $ID"
echo "Please su to root and run script again..."
echo "Exiting...";echo
exit
fi

###Mount /dev/hda4

echo "Checking entries in mtab..."
ENTRY= 'cat /etc/mtab | awk '{print $1;}' | grep "/dev/hda4"'
ENTRY_WHERE= 'cat /etc/mtab | awk '{print $1;}' | grep "/home/user/mountpoint"'
if [ "$ENTRY" = "/dev/hda4" ]
then
echo; echo "Already Mounted at [$ENTRY_WHERE] Exiting...";echo
else
echo; echo "Mounting windows from [$ENTRY] at [$ENTRY_WHERE]..."
mount -t vfat /dev/hda4 /home/user/mountpoint/
echo
echo "Mounted..."
exit
fi
The problem is, when I run the commands individually, they work fine, but in the script, i get errors..

Example:

Code:
 id | awk '{print $1;}'|grep "uid=0"
gives "uid=0(root)", but when i run it in the script, it gives me something like

---

/home/user/script.sh: line 6: }|grep "uid=0": command not found
id | awk {print

---
The variable doesnt seem to be getting the value at all....

I'm really very new in programing, but certainly not new to Linux...

The script seems fine, no typos, no syntax errors, etc...but i just cant figure out whats wrong...

Any help will be appreciated

king_nothingzzz
 
Old 06-15-2004, 07:42 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
You're using the wrong marks for your variable assignments. I assume that you want to assign the ID variable the value of the command sequence after the "=". To do that, you need to use the backtick character ` (not the single quote character ').

You should be able to use either of these forms:
Code:
ID=`cat /etc/mtab | awk '{print $1;}' | grep "/dev/hda4"`
ID=$(cat /etc/mtab | awk '{print $1;}' | grep "/dev/hda4")
Your other variable assignments later will need similar modifications.
 
Old 06-15-2004, 08:16 PM   #3
king_nothingzzz
Member
 
Registered: Aug 2003
Location: Brisbane, Australia
Distribution: Red Hat Enterprise Linux 4
Posts: 120

Original Poster
Rep: Reputation: 15
Gee thanks...

Now i know the mistake....

Thanks a lot Dark_Helmet

Cheers

king_nothingzzz
 
Old 06-18-2004, 10:04 PM   #4
seeLnd
LQ Newbie
 
Registered: May 2004
Location: Beijing, China
Distribution: Debian
Posts: 16

Rep: Reputation: 0
I tried your code on debian sarge(bash 2.05b), but not found any wrong.

You can add an option with id command like this:
Code:
id -u
then you'll get only one string: user id

Last edited by seeLnd; 06-18-2004 at 10:07 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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Problems with simple script.... jong357 Programming 6 12-20-2004 06:08 AM
problems creating a simple bootable cd to perform a simple task czarherr Linux - Software 1 11-11-2004 05:22 AM
A Simple Script fiod Linux - General 3 09-20-2003 08:56 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration