help needed with script that gets a filename as argument (with an if/else statement)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help needed with script that gets a filename as argument (with an if/else statement)
# 1  
Old 04-01-2009
help needed with script that gets a filename as argument (with an if/else statement)

Hi,

I am trying to create a script just to study BASH scripting, but I have some problems.

I need to create a script that gets a filename as an argument. The script should behave as follows:
• If the given file name already exists, inform the user and quit.
• If the given file name does not exist, create an empty file by that name and inform the user.

This is my start:

Code:
#!/bin/sh
echo "type in a filename:"
read $FILENAME

if 

....

else

#filename that does not exist is created
echo "The filename $FILENAME does not exist, creating empty file $FILENAME"
touch $FILENAME

# 2  
Old 04-01-2009
Code:
if [ -f $file ]
    ...
else
    touch $file

man test has a few more checks for files, variables, ....
# 3  
Old 04-02-2009
Hi,

Thanks for the help I found out after trying and using google that this is the way to do it:

Code:
#!/bin/sh
echo "Please type in a filename:"
read filename
if
        [ -f ./$filename ];
then
        #a simpele display message that the file exist
        echo "file exists"
else
        #this part creates the file if the inputfilename does not exist
        echo "file does not exist, file is created"
        touch $filename
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python passing filename through argument

Hello, A python beginner question on passing filename thru argument. My code is: #!/usr/bin/python import sys, getopt import os def main(argv): try: opts, args = getopt.getopt(sys.argv,"hi:o:ce", ) except getopt.GetoptError: usage() print("Usage: %s... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Shell Programming and Scripting

bash if statement help needed

Hi I need a script with an if statement that goes. I need it to search through all files within a directory with the extension .test if it finds the string '71502FSC1206' then do sed 's/71502FSC1206/\n&/g' > send.test If it finds the string '715MCH' or '715JAC' then I need it to move the... (1 Reply)
Discussion started by: firefox2k2
1 Replies

3. Shell Programming and Scripting

Expect Scripting Loop Argument Desperately Needed!

I am trying to create an Expect script that does the following: 1) Telnets to an IP address and logs in with user ID and Password 2) Issue a CLI command to the server that will output data of which I am particularly interested in a DS1 clock 'Slips' value. I want to be able to keep issuing... (0 Replies)
Discussion started by: dwightlaidler
0 Replies

4. Shell Programming and Scripting

How to pass a filename as a command line argument

Hi,I have a script which is given below :#!/bin/bash. ini_script.shdb2 connect to $DB_NAME user $DB2_UID using $DB2_PASSWORDfor file in `ls -1 ./sql/ddw/`do echo "Executing the file $file" echo db2 -tvf $filedonedb2 quiti want this script to accept directorie's names present in... (1 Reply)
Discussion started by: ektubbe
1 Replies

5. Shell Programming and Scripting

filename change with awk needed

Hi, i have files which contains list of csv file names say temp.txt contains below like data Dns_bangalore_08172011.093033.1139.csv Dns_bangalore_08172011.093133.1139.csv now i want to insert some string before .csv in the filename say i want to insert string _sim1 beofre .csv... (3 Replies)
Discussion started by: raghavendra.nsn
3 Replies

6. 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

7. Shell Programming and Scripting

use input filename as an argument to name output file

I know this is a simple matter, but I'm new to this. I have a shell script that calls a sed script from within it. I want the output of the shell script to be based on the input file I pass as an argument to the original script. In other words... ./script.sh file.txt (script.sh calls sed... (2 Replies)
Discussion started by: estebandido
2 Replies

8. 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

9. Shell Programming and Scripting

help needed in using case statement

Hi, I have a script as below: ....................................................................... rpttxt() { name="$*" awk '/'"${name}"'/ {print $2$3$4 }' file_1.txt } title="`rpttxt "TITLE"`" ......................................................................... The... (0 Replies)
Discussion started by: jisha
0 Replies

10. UNIX for Dummies Questions & Answers

pass argument to a filename

How can I use the value of an argument as a filename? Example: The argument for a process is 999. I would like the output of the process to be placed in a file called 999. I have tried using $$1, but that only assigns a unigue number. thanks JP (1 Reply)
Discussion started by: jpprial
1 Replies
Login or Register to Ask a Question