test-and-set in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test-and-set in bash
# 1  
Old 06-17-2008
test-and-set in bash

I'm not talking about the assembly instruction TAS, a better name could be check-and-set Smilie Anyway, is there a way to simplify the following

Code:
if [ -z $VAR ]; then VAR="something"; fi

I have ~20 variables that should be test-and-set like this, and it really looks lame.
# 2  
Old 06-17-2008
a shorthand could be

Code:
[ -z $VAR ] && VAR="something"

# 3  
Old 06-17-2008
Code:
: ${VAR:=something} ${ANOTHER:=whatever}

The semantics of the := variable substitution operator are not exactly equivalent to what you're asking about, but perhaps it's close enough.

Note that the default values are often set on a line which begins with the colon (basically "nop") shell command. It's different from the comment in that the rest of the line is evaluated by the shell. Anyway, see your shell's manual page for details.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash condition test at the end of string

I want to check (using bash condition test function) if string contains three spaces, ignoring last three spaces at the end of string. string_to_report='foo bar foo bar ' string_to_ignore='foo bar ' (8 Replies)
Discussion started by: useretail
8 Replies

2. Shell Programming and Scripting

Operator test in bash

Hello, can you please help me because I am totally confused with a simple script: #!/bin/bash ] || ] && echo "Good Morning" ] || ] && echo "Good Night" For me, these two strings are indentical: false || false and there is no point to execute echo command. But the run result is... (5 Replies)
Discussion started by: AndreiM
5 Replies

3. Shell Programming and Scripting

if test in bash - can't see error

Hi, I have the following script under bash if ; then echo File $file_name Not Found else echo File $file_name Found fi I get the message even the file is found; there is only one file with that name in the directory I can't figure... (4 Replies)
Discussion started by: f_o_555
4 Replies

4. UNIX for Advanced & Expert Users

selective set of tests in gcc-4.5.1 test suite

Hi, I am unable to run a selective set of tests in gcc-4.5.1 test suite. I tried two runtest commands given below (output is also given below). Can you please guide me how to run specific set of tests for example : I would like to run only tests gcc.target/i386/avx-vaddpd-1.c and ... (0 Replies)
Discussion started by: ganesh2282
0 Replies

5. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

bash script to test network connection - please help

I want to test if my host can connect to any of the following 10 hosts (192.168.1.0 to 192.168.1.9) I didnt know which command i should use, so i chose ping. (i wonder if i should use tracepath or sth else) my code is the following: #!/bin/bash clear firstPart="192.168.1" maxNum="9" ... (2 Replies)
Discussion started by: shadow_boi
2 Replies

8. Shell Programming and Scripting

FIle (directory) test operator (bash)

I'm almost pulling out my hair trying to figure out what's wrong with this... there's no reason I can see that it shouldn't be working. It seems that the code acts as though the conditional statement is true no matter what - I've even tried removing the negation operator, but it always goes into... (5 Replies)
Discussion started by: wildbluefaerie
5 Replies

9. Shell Programming and Scripting

[bash]regexp to test PrintableString

Hello all, I would like some help to find the regexp to test that a word does not contains anything than the character set of printable strings. Especially the word should not contain a underscore. echo "$word" | grep "( \'\(\)+,-\.:=\?)+" if then echo success else echo failed... (2 Replies)
Discussion started by: dolphin06
2 Replies

10. Shell Programming and Scripting

regex test in bash

Hi I want to do a regex test and branch based on the test result, but this doesn't seems to work :confused: if \) ]] then echo success else echo failed fi (1 Reply)
Discussion started by: subin_bala
1 Replies
Login or Register to Ask a Question