Difference between command line and script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between command line and script
# 1  
Old 02-02-2013
Difference between command line and script

i have a bit of a unique question. is there a way to know if a script is being run from from the command line or from another script?

example:
Code:
command line:

### ./autorun.sh
ERROR: not permitted to run script from the commandline. exiting...

but if i put "autorun.sh" into another script and call it "master.sh", i want the master.sh script to know when the user is using the "master.sh" script itself to run the "autorun.sh" and when the user decides to run the "autorun.sh" separately.

the preference here is to prevent the user from runnign the the autorun.sh separately. but i'm lost as to what i can use to identify when it is being run from the command line or through the master.sh script.

os: linux/sunos
shell: bash
# 2  
Old 02-02-2013
Try this:
Code:
$ cat master.sh
./autorun.sh &
wait

Code:
$ cat autorun.sh
if tty -s
then
        echo "ERROR: not permitted to run script from the commandline. exiting..."
        exit 1
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-02-2013
this works beautifully. one last question,

if i wanted to make it so a script can only be run by one particular user, is that possible? meaning in addition to what you posted, can something be worked into the script to make it so that, for instance, userA is the only person that can run a script. meaning, userA has to be logged into the system, not some other user who "su -" to become userA.
# 4  
Old 02-02-2013
Note: tty -s has been deprecated. An alternative is:
Code:
if [ -t 0 ]; then
  echo "ERROR: not permitted to run script from the commandline. exiting..."
  exit 1
fi

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-02-2013
Quote:
Originally Posted by SkySmart
if i wanted to make it so a script can only be run by one particular user, is that possible? meaning in addition to what you posted, can something be worked into the script to make it so that, for instance, userA is the only person that can run a script. meaning, userA has to be logged into the system, not some other user who "su -" to become userA.
Code:
user=$( who am i )
user=${user%% *}
[[ "$user" != "SkySmart" ]] && exit 1

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Works on command line but not in script

OSX 10.9 I am building a script that evaluates the difference between 2 files. Here is a command that does not work transparently. Running this command in Terminal yields great results; however when I put that line in a .sh script, I get the errors shown below. Am I doing something silly? ... (1 Reply)
Discussion started by: sudo
1 Replies

2. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

3. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

4. Shell Programming and Scripting

how to convert this script in command line?

im trying to use this shellscript in one command line, but isnt working. shellscript: SERVER=ftp.site.com USER=Foo PASSW=3122 ftp -v -n $SERVER <<END_OF_SESSION user $USER $PASSW $FILETYPE lcd /home/foo/mywebsite mput * mput *.* bye END_OF_SESSION Im trying a lot of... (7 Replies)
Discussion started by: fbs777
7 Replies

5. Shell Programming and Scripting

Command executes on the command line but not from script

Hello guys, I have the following commands : Command 1: sudo find "../bundlepool" -name "merchandising2.html" -print0 |xargs -0 -I {} cp "rebranding/merchandising2.html" {} Command 2: find "../bundlepool" -name "merchandising2.html" -exec cp rebranding/merchandising2.html {} \; Command 3: ... (2 Replies)
Discussion started by: ihabo01
2 Replies

6. Shell Programming and Scripting

Help with script or command to differentiate difference between two input file?

I got two file write now. Input file 1: >data_1 DSFDFDSGFDSGSGEGTRTRERPOYIORPGKKGDSPKFSDKFPSDKFSPFS >data_34 WEEREREWREWOIQOPIEPDSKLFNDSFNSKNCASKJHDAFHAOUDFEOWWIOUFEWIUEWIRUEWIRUEWIORUEWOREWR >data_21 ASDASDQWEQWRQERFWPOTGIUWEIPOFIOFDSNFKSJDNFSKDHFKDSJHFKDSJHF >data_4... (14 Replies)
Discussion started by: perl_beginner
14 Replies

7. Shell Programming and Scripting

perl script command line option driven script

could someone show me a sample command line option driven script? i want to see an easy way to write one and how i can execute it using command line options such as typing in read.pl -i <id> -c <cmds> -s <start> -e <end> would read out all the commands run by ID . from start time to... (7 Replies)
Discussion started by: kpddong
7 Replies

8. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

9. Shell Programming and Scripting

Line by Line Comparision of 2 files and print only the difference

Hi, I am trying to find an alternative way to do tkdiff. In tkdiff the gui compares each line and highlights only the differences. for eg: John works at McDonalds s(test) He was playing guitar tywejk John works in McDonalds 9908 He was playing guitar I am... (1 Reply)
Discussion started by: naveen@
1 Replies

10. UNIX for Dummies Questions & Answers

Script through cron and command line

I have a script which runs fine through command line, but doesn't run through cron. There are some variables which are set by the .profile file which are used by the script. Is it that cront does not pick these variables. $/export/home/rahul/bin/createfile.sh >>... (3 Replies)
Discussion started by: rahulrathod
3 Replies
Login or Register to Ask a Question