First line of a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting First line of a script
# 1  
Old 05-16-2014
First line of a script

so we all know the first line of a shell script has to have the shell defined as in:
Code:
#!/bin/bash

so, how do i have this automatically changed in a script so a certain shell is used, given the OS the script is running on?

i tried making this the very first few lines in the script:

Code:
UNAMEA=$(uname)
if [ "${UNAMEA}" = "SunOS" ] ; then
        echo "#!/usr/xpg4/bin/bash"
else
        echo "#!/bin/bash"
fi

which kicks out errors such as this:

Code:
script.sh: line 4: syntax error near unexpected token `else'
script.sh: line 4: `else'

any ideas on how to accomplish what i'm trying to do?
# 2  
Old 05-16-2014
You can't put variables in the shebang. You could use the following depending on if you have the right bash path in your $PATHvariable:
Code:
#!/usr/bin/env bash

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 05-16-2014
Someone should also point out that people write scripts for a particular shell. If I write a script for ksh, I can't be certain that it will run as is in bash. At least not without testing first.

You can create a sym link such as /usr/local/bin/bash that points to /bin/bash. Then use /usr/local/bin/bash as your shebang. This could give you the option of pointing it to /bin/zsh if you want. But making that kind of change could still break some or all of your scripts.
This User Gave Thanks to gandolf989 For This Post:
# 4  
Old 05-16-2014
Or instead of
Code:
/path/to/yourscript

run your script like this:
Code:
env PATH=/usr/xpg4/bin:/usr/bin bash /path/to/yourscript

The shebang is ignored then.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 05-16-2014
Quote:
Originally Posted by MadeInGermany
Or instead of
Code:
/path/to/yourscript

run your script like this:
Code:
env PATH=/usr/xpg4/bin:/usr/bin bash /path/to/yourscript

The shebang is ignored then.
this looks like what i'm looking for. is there anyway i can have that run in the script automatically? the goal here is to have the user run just one script without having to type anything extra.

i tried this:

Code:
#!$(env PATH=/usr/xpg4/bin:/usr/bin:/bin sh)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

3. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

4. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

5. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

6. Shell Programming and Scripting

linux shell script to take variables from two different files line by line

Friends I need to have a shell script which will feed variables from two different files line-by-line. For example, I have two files - permission and file_name. Contents of permission is - 644 755 .... contents of file_name /file1 /file2 ..... Now I want 644 permission will be... (4 Replies)
Discussion started by: atanubanerji
4 Replies

7. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

8. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

9. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

10. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies
Login or Register to Ask a Question