Bash if statements in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash if statements in one line
# 8  
Old 03-27-2012
I like the shell built-ins true and false.
Code:
hasArgument1=true
hasArgument2=false

if $hasArgument1; then
  echo has argument 1
fi

if ! $hasArgument2; then
  echo does not have argument 2
fi

# 9  
Old 03-27-2012
Quote:
Originally Posted by Scrutinizer
I like the shell built-ins true and false.
Code:
hasArgument1=true
hasArgument2=false

if $hasArgument1; then
  echo has argument 1
fi

if ! $hasArgument2; then
  echo does not have argument 2
fi

Sounds a good thing for my scripts when I am using hasArgument, rather than checking if it is 0 or 1 all the time. Correct me if I am wrong.

---------- Post updated at 01:56 PM ---------- Previous update was at 11:36 AM ----------

Currently I have a lot of options in the scripts, as shown below.

Thus I end up having hasArgument appended to every option. I am seriously thinking of having a function which I will call

Quote:
hasArgument
which will be called as

Quote:
hasArgument(optionName)

Example:

Quote:
hasArgument("Srcs")
returns true if the option Scrs was supplied by the user.

Is it possible to create such a function call and how would I use it. Seems a good idea at the moment, but would like some input on it.

I have also been thinking of using an associative array hasArgument

For example

Code:
hasArgument[Srcs]=true     # Option Srcs was set by the user.

Code:
hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0
hasArgumentRaysOutfile=0
hasArgumentTrvTOutfile=0

ierr=0
hasArgumentRaytracPath=0
hasArgumentPhases=0
hasArgumentLevel=0
hasArgumentFormat=0
hasArgumentDtau=0
hasArgumentBracD=0
hasArgumentTwPtD=0
hasArgumentTwPtItmax=0
hasArgumentRays=0
hasArgumentTrvT=0
hasArgumentPf=0
hasArgumentRaytracBg=0
hasArgumentRaytracVrbLevel=0

hasArgumentVerbose=0
hasArgumentQuiet=0
hasArgumentUsage=0
hasArgumentExamples=0
hasArgumentHelp=0

hasArgumentBDirPath=0
hasArgumentBDirColPos=0
hasArgumentBDirSortF=0
hasArgumentBDirGroupT=0
hasArgumentBDirAllFiles=0
hasArgumentBDirRaytrac=0
hasArgumentBDirDrw=0
hasArgumentBDirSmp=0
hasArgumentBDirLog=0
hasArgumentBDirMsf=0
hasArgumentBDirVrbLevel=0
hasArgumentBDirQuiet=0
hasArgumentBDirVerbose=1             # Default verbosity for script browseDir.tcsh
hasArgumentBDirFileLst=0

...

if [ $hasArgumentSrcsInfile -eq 1 ]; then

fi

if [ $hasArgumentRcvsInfile -eq 1 ]; then
  ...
fi

if [ $hasArgumentPhases -eq 1 ]; then
  ...
fi

if [ $hasArgumentLevel -eq 1 ]; then
  ...
fi

if [ $hasArgumentFormat -eq 1 ]; then
  ...
fi

if [ $hasArgumentDtau -eq 1 ]; then
  ...
fi

if [ $hasArgumentBracD -eq 1 ]; then
  ...
fi

if [ $hasArgumentTwPtD -eq 1 ]; then
  ...
fi

if [ $hasArgumentTwPtItmax -eq 1 ]; then
  ...
fi

if [ $hasArgumentRaysOutfile -eq 1 ]; then
  ...
fi

if [ $hasArgumentTrvTOutfile -eq 1 ]; then
  ...
fi

---------- Post updated at 02:40 PM ---------- Previous update was at 01:56 PM ----------

Last edited by kristinu; 03-27-2012 at 01:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One Line Command how to use pipe statements to execute and comment on multiple possible outcomes

Hello Forum, I'm looking to expand the following command: INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server") Currently this command will check if my server is RedHat server using the grep -qi... (6 Replies)
Discussion started by: greavette
6 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

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

4. Shell Programming and Scripting

Equivalence of "if then" statements in bash

Would these two sections of code be equivalent? The original is the first one, followed by the new implementation. if ; Then ... else if ; Then ... fi fi if ; Then ... else if ; Then ... fi (4 Replies)
Discussion started by: kristinu
4 Replies

5. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

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

7. Shell Programming and Scripting

Multiple If statements in bash problem

Hi everyone, May you help me with the correct syntax of the follow bash statements please X=10 if ]; then echo "The value is between 1 and 5" for ((i=1;i<=${X})); do echo $i done else if ]; then echo "The value is between 6 and 10" for ((i=1;i<=${X})); do ... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

8. Shell Programming and Scripting

Errors in bash with if statements

Hello everyone, I got this type of error when programming in bash new.bat: 16: cannot create : Directory nonexistent $bool new.bat: 37: Syntax error: "then" unexpected (expecting "fi") Does anyone know why? Here is my code #!bin/bash #function helps(){... (4 Replies)
Discussion started by: bbbash
4 Replies

9. Shell Programming and Scripting

how can i write the following statements on one line?

Hi, At the moment, I have the following statements which show like this, but I want it all on one line. How can I do this? ---------------------------- echo "There are" cat menu | wc -l echo "characters in the text" ---------------------------- Thanks! (5 Replies)
Discussion started by: kev269
5 Replies

10. Shell Programming and Scripting

How to place the output of two different echo statements on one line

Hello there, I wrote a shell script to modify the code for some of our clients in our client database. Before starting the data modification the program performs a few checks. When a check is being performed, it should be shown on the screen of the user running the program, the result of... (5 Replies)
Discussion started by: JoBa
5 Replies
Login or Register to Ask a Question