Sending an argument to a .sh file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Sending an argument to a .sh file
# 1  
Old 04-11-2018
Sending an argument to a .sh file

Hi,

This is the content of a file name "test.sh":

Code:
#!/bin/bash
if [ ! -f $2 ]; then
 echo "I am Here"
fi

When runing the command:
Code:
./test.sh chair table

The echo command "I am Here" will appear only based on the second value, in other words only if the there is no file name "table" regardless if there isn't a file name "chair".. Why is that ?

B.What exactly did we done here ?. We sent an argument to a file containing shell commands ?
# 2  
Old 04-11-2018
Quote:
Originally Posted by uniran
Hi,

This is the content of a file name "test.sh":

Code:
#!/bin/bash
if [ ! -f $2 ]; then
 echo "I am Here"
fi

When runing the command:
Code:
./test.sh chair table

The echo command "I am Here" will appear only based on the second value, in other words only if the there is no file name "table" regardless if there isn't a file name "chair".. Why is that ?

B.What exactly did we done here ?. We sent an argument to a file containing shell commands ?
Did you notice the $2 that I marked in red in your script? Do you see that the test is looking for a file named by the 2nd command-line argument passed to your script?

What happens if you change the $2 to $1 and rerun your script at times when there is a file named chair and at times when there is not a file named chair?

What do you think might happen if you changed the line in your script that is currently:
Code:
if [ ! -f $2 ]; then

to:
Code:
if [ ! -f "$1" ] && [ ! -f "$2" ]; then

or to:
Code:
if [ ! -f "$1" ] || [ ! -f "$2" ]; then

? Note that I added double quotes around each parameter expansion. With the files named in your example and the default characters specified in "$IFS", it doesn't matter. But you should always write you code to protect yourself in case an argument is passed to your script that contains <space>s, <tab>s, or <newline>s.

Last edited by rbatte1; 04-11-2018 at 01:16 PM.. Reason: Spelling
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

2. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

3. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

4. Shell Programming and Scripting

use argument file with sed command

I am trying to delete lines from a file with the sed command. I have two files. File a.txt 200,000 records. Here is a sample of the file. File a.txt 2401ABC0212345678 plus 250 characters of other data 2401ABC0212345678 plus 250 characters of other data 2402ABC0212345678 plus... (3 Replies)
Discussion started by: robbanigan
3 Replies

5. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

6. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies

7. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

8. UNIX for Dummies Questions & Answers

how do I insert argument into TOP of file using vi?

when directing some text into a file can you choose where it goes like the top of the file (which is text aswell) or the middle?? if so how - especially would like to know how to do so in vi (text editor) If i were to enter an argument ($1) into a another argument ($2) would it would be... (6 Replies)
Discussion started by: rprules
6 Replies

9. Shell Programming and Scripting

ls and take each file as argument

I have LOGFILES=`ls *.log` and want to egrep each entry of LOGFILES one after the other: egrep $LOGFILE.... how to do that?? thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

10. UNIX for Dummies Questions & Answers

Inserting argument into top of a file

Hi, I am new to Unix, and I am trying to append a line of argument into a current file. I need this line to be inserted into the very top of the file. Does anyone know how this is done? For example, I am trying: echo "insert to top" >> filename. This inserts the line at the bottom of the... (7 Replies)
Discussion started by: Dev06
7 Replies
Login or Register to Ask a Question