if variable is not set then use default value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if variable is not set then use default value
# 1  
Old 12-02-2010
if variable is not set then use default value

Hi everybody. I am finishing this program but there is just one detail.
I need to define the number of files that have to be deleted in this way MAXFILES=3 ./program 1 2 3 4 if the $# is > MAXFILES then confirm otherwise just delete them. But if i don't define the MAXFILES like ./program 1 2 3 4 then there is a default number which is 10. more than 10 confirm otherwise just delete them. But the program that I have so far just works with the MAXFILES and I am not sure how to make it work with both ways. Here it is. Thank you very much in advanced.

Code:
i=10
x=$MAXFILES

if [ "$x" -ne 10 ]
    then
if [ "$#" -gt "$x" ]
    then
      echo "Remove $# files (y/n)?"
      read ans
         if [ "$ans" != n ]
             then
                rm "$@"
                echo "Files removed!"
         else
             echo "Files not removed"
         fi
     else
     rm "$@"
     echo "Files have been removed!"
fi
else
    if [ "$#" -eq 10 ]
        then
        echo "Remove $# files (y/n)?"
      read ans
         if [ "$ans" != n ]
             then
                rm "$@"
                echo "Files removed!"
         else
             echo "Files not removed"
         fi
     else
     rm "$@"
     echo "Files have been removed!"
   fi
fi


Last edited by bartsimpsong; 12-02-2010 at 06:03 AM.. Reason: I forgot a detail, thanks
# 2  
Old 12-02-2010
Code:
$ x=$MAXFILES
$ echo $x

$ x=${MAXFILES:-10}
$ echo $x
10
$ MAXFILES=5
$ x=${MAXFILES:-10}
$ echo $x
5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Use read to set a variable.

Hi all, I used to set variable by read from keyboard read -p 'Input new value for variable :' var Now I want to pipe from ls and set to var a.txt b.txt c.txt ls | grep a.txt | read var why this cannot set the $var. What is the different between them....:wall: (4 Replies)
Discussion started by: mainsun
4 Replies

2. Shell Programming and Scripting

do you have a better way to set this variable?

greetings, i have a variable $input that i want to use to set $output. $input is /dir/filename.mph and $input is passed to my script that i manipulate it as follows: input=`basename $input`i want the $output to be filename_solved.mph, basically stuffing "_solved" in the filename. here's how i... (2 Replies)
Discussion started by: crimso
2 Replies

3. Shell Programming and Scripting

How to know who and where a variable is set ?

hi, i'm not a root user and i want to know which user and in which file is loaded a variable seen in the "env" display ? I will use this variable but i want to be sure that it will be a permanent variable ! i don't see it in my files (.profile , kshrc...) and neither in /etc/profile. ... (3 Replies)
Discussion started by: Nicol
3 Replies

4. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

5. Shell Programming and Scripting

How to set value to variable in UNIX

Hi, I am new to UNIX. I wonder how to set value to variable in UNIX.. my code: if ; then EXIST=$a ; else EXIST=$b ; fi' echo $EXIST the value of EXIST is empty... Thanks :) (5 Replies)
Discussion started by: suigion
5 Replies

6. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

7. Solaris

set environment variable?

I am working with solaris 9 sunBlade150 Box. I Installed a program, need to set the environment variable so that when the executable is entered,it finds the path to the executable. The documentation for the software says: Set the appropriate environment variable: Connect to server failed;... (8 Replies)
Discussion started by: smartgupta
8 Replies

8. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

9. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

10. Programming

TZ variable set within thread

Hello all, I'm going to be using some of the date functions from time.h to do some time stamping. I will be getting a time and date from the header of a TIFF file. I will need to be able create a time for each time zone in the U.S. The source of the time stamp will be in GMT. What I'd like to do... (2 Replies)
Discussion started by: shldBcding
2 Replies
Login or Register to Ask a Question