Sponsored Content
Full Discussion: Substitution in a variable
Top Forums UNIX for Advanced & Expert Users Substitution in a variable Post 8800 by LivinFree on Thursday 18th of October 2001 12:15:44 AM
Old 10-18-2001
First thing I noticed, was that you would echo out $Cat_name. You didn't define Cat_name. The other thing I did was to add another \ to the sed statement, along with the -e option... Hope that helps...

Try this:
Code:
#!/bin/sh

var_name="Hello World"
echo "var_name is $var_name"
var_name=`echo $var_name | sed -e 's/ /\\\ /g'`
echo "var_name has changed to $var_name"

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

2. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

3. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

4. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

5. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

6. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

7. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

8. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies

9. Shell Programming and Scripting

read variable substitution

Allright so a quick question. I'm building a script that will eventually do a full IP subnet scan. It starts off by first entering an IP address, (capturing host and net ID comes after that) and I want it to use the current IP address if no input is given. Is there a quick way to define the... (1 Reply)
Discussion started by: BisMarc
1 Replies

10. Shell Programming and Scripting

Variable substitution with arrays

Hi all, I have a script with the following gist: declare -a index=(0 1 2 3 4); declare -a animals=(dog cat horse penguin cow); declare -a fruits=(orange apple grapes peach mango); declare -a drinks=(juice milk coffee tea coke); declare -a cities=(toronto paris london glasgow... (18 Replies)
Discussion started by: Kingzy
18 Replies
ATF-SH(3)						   BSD Library Functions Manual 						 ATF-SH(3)

NAME
atf_add_test_case, atf_check, atf_check_equal, atf_config_get, atf_config_has, atf_expect_death, atf_expect_exit, atf_expect_fail, atf_expect_pass, atf_expect_signal, atf_expect_timeout, atf_fail, atf_get, atf_get_srcdir, atf_pass, atf_require_prog, atf_set, atf_skip, atf_test_case -- POSIX shell API to write ATF-based test programs SYNOPSIS
atf_add_test_case "name" atf_check "command" atf_check_equal "expected_expression" "actual_expression" atf_config_get "var_name" atf_config_has "var_name" atf_expect_death "reason" "..." atf_expect_exit "exitcode" "reason" "..." atf_expect_fail "reason" "..." atf_expect_pass "" atf_expect_signal "signo" "reason" "..." atf_expect_timeout "reason" "..." atf_fail "reason" atf_get "var_name" atf_get_srcdir atf_pass atf_require_prog "prog_name" atf_set "var_name" "value" atf_skip "reason" atf_test_case "name" "cleanup" DESCRIPTION
ATF provides a simple but powerful interface to easily write test programs in the POSIX shell language. These are extremely helpful given that they are trivial to write due to the language simplicity and the great deal of available external tools, so they are often ideal to test other applications at the user level. Test programs written using this library must be run using the atf-sh(1) interpreter by putting the following on their very first line: #! /usr/bin/env atf-sh Shell-based test programs always follow this template: atf_test_case tc1 tc1_head() { ... first test case's header ... } tc1_body() { ... first test case's body ... } atf_test_case tc2 cleanup tc2_head() { ... second test case's header ... } tc2_body() { ... second test case's body ... } tc2_cleanup() { ... second test case's cleanup ... } ... additional test cases ... atf_init_test_cases() { atf_add_test_case tc1 atf_add_test_case tc2 ... add additional test cases ... } Definition of test cases Test cases have an identifier and are composed of three different parts: the header, the body and an optional cleanup routine, all of which are described in atf-test-case(4). To define test cases, one can use the atf_test_case function, which takes a first parameter specifiying the test case's name and instructs the library to set things up to accept it as a valid test case. The second parameter is optional and, if provided, must be 'cleanup'; providing this parameter allows defining a cleanup routine for the test case. It is important to note that this function does not set the test case up for execution when the program is run. In order to do so, a later registration is needed through the atf_add_test_case function detailed in Program initialization. Later on, one must define the three parts of the body by providing two or three functions (remember that the cleanup routine is optional). These functions are named after the test case's identifier, and are <id>_head, <id>_body and <id>_cleanup. None of these take parameters when executed. Program initialization The test program must define an atf_init_test_cases function, which is in charge of registering the test cases that will be executed at run time by using the atf_add_test_case function, which takes the name of a test case as its single parameter. This main function should not do anything else, except maybe sourcing auxiliary source files that define extra variables and functions. Configuration variables The test case has read-only access to the current configuration variables through the atf_config_has and atf_config_get methods. The former takes a single parameter specifying a variable name and returns a boolean indicating whether the variable is defined or not. The latter can take one or two parameters. If it takes only one, it specifies the variable from which to get the value, and this variable must be defined. If it takes two, the second one specifies a default value to be returned if the variable is not available. Access to the source directory It is possible to get the path to the test case's source directory from anywhere in the test program by using the atf_get_srcdir function. It is interesting to note that this can be used inside atf_init_test_cases to silently include additional helper files from the source direc- tory. Requiring programs Aside from the require.progs meta-data variable available in the header only, one can also check for additional programs in the test case's body by using the atf_require_prog function, which takes the base name or full path of a single binary. Relative paths are forbidden. If it is not found, the test case will be automatically skipped. Test case finalization The test case finalizes either when the body reaches its end, at which point the test is assumed to have passed, or at any explicit call to atf_pass, atf_fail or atf_skip. These three functions terminate the execution of the test case immediately. The cleanup routine will be processed afterwards in a completely automated way, regardless of the test case's termination reason. atf_pass does not take any parameters. atf_fail and atf_skip take a single string parameter that describes why the test case failed or was skipped, respectively. It is very important to provide a clear error message in both cases so that the user can quickly know why the test did not pass. Expectations Everything explained in the previous section changes when the test case expectations are redefined by the programmer. Each test case has an internal state called 'expect' that describes what the test case expectations are at any point in time. The value of this property can change during execution by any of: atf_expect_death "reason" "..." Expects the test case to exit prematurely regardless of the nature of the exit. atf_expect_exit "exitcode" "reason" "..." Expects the test case to exit cleanly. If exitcode is not '-1', the runtime engine will validate that the exit code of the test case matches the one provided in this call. Otherwise, the exact value will be ignored. atf_expect_fail "reason" Any failure raised in this mode is recorded, but such failures do not report the test case as failed; instead, the test case final- izes cleanly and is reported as 'expected failure'; this report includes the provided reason as part of it. If no error is raised while running in this mode, then the test case is reported as 'failed'. This mode is useful to reproduce actual known bugs in tests. Whenever the developer fixes the bug later on, the test case will start reporting a failure, signaling the developer that the test case must be adjusted to the new conditions. In this situation, it is useful, for example, to set reason as the bug number for tracking purposes. atf_expect_pass This is the normal mode of execution. In this mode, any failure is reported as such to the user and the test case is marked as 'failed'. atf_expect_signal "signo" "reason" "..." Expects the test case to terminate due to the reception of a signal. If signo is not '-1', the runtime engine will validate that the signal that terminated the test case matches the one provided in this call. Otherwise, the exact value will be ignored. atf_expect_timeout "reason" "..." Expects the test case to execute for longer than its timeout. Helper functions for common checks atf_check "[options]" "command" "[args]" Executes a command, performs checks on its exit code and its output, and fails the test case if any of the checks is not successful. This function is particularly useful in integration tests that verify the correct functioning of a binary. Internally, this function is just a wrapper over the atf-check(1) tool (whose manual page provides all details on the calling syn- tax). You should always use the atf_check function instead of the atf-check(1) tool in your scripts; the latter is not even in the path. atf_check_equal "expected_expression" "actual_expression" This function takes two expressions, evaluates them and, if their results differ, aborts the test case with an appropriate failure message. The common style is to put the expected value in the first parameter and the actual value in the second parameter. EXAMPLES
The following shows a complete test program with a single test case that validates the addition operator: atf_test_case addition addition_head() { atf_set "descr" "Sample tests for the addition operator" } addition_body() { atf_check_equal 0 $((0 + 0)) atf_check_equal 1 $((0 + 1)) atf_check_equal 1 $((1 + 0)) atf_check_equal 2 $((1 + 1)) atf_check_equal 300 $((100 + 200)) } atf_init_test_cases() { atf_add_test_case addition } This other example shows how to include a file with extra helper functions in the test program: ... definition of test cases ... atf_init_test_cases() { . $(atf_get_srcdir)/helper_functions.sh atf_add_test_case foo1 atf_add_test_case foo2 } This example demonstrates the use of the very useful atf_check function: # Check for silent output atf_check -s exit:0 -o empty -e empty 'true' # Check for silent output and failure atf_check -s exit:1 -o empty -e empty 'false' # Check for known stdout and silent stderr echo foo >expout atf_check -s exit:0 -o file:expout -e empty 'echo foo' # Generate a file for later inspection atf_check -s exit:0 -o save:stdout -e empty 'ls' grep foo ls || atf_fail "foo file not found in listing" # Or just do the match along the way atf_check -s exit:0 -o match:"^foo$" -e empty 'ls' SEE ALSO
atf-check(1), atf-sh(1), atf-test-program(1), atf-test-case(4) BSD
October 13, 2014 BSD
All times are GMT -4. The time now is 01:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy