Sponsored Content
Full Discussion: Trim a new line
Top Forums Shell Programming and Scripting Trim a new line Post 302459127 by alister on Sunday 3rd of October 2010 06:33:27 PM
Old 10-03-2010
Quote:
Originally Posted by tnanek
What I want is just the IP address, no line breaks before or after, in a variable that I can then insert into my local host file, after some tweaking of adding my desired access domain to it.

Command substitution trims trailing newlines, so I don't even understand how you're having a problem with a trailing newline(s) in a variable whose value is assigned from command substitution.

In the following shell session, while the first printf statement generates 5 trailing newlines (verified with od), not a single one is assigned to the variable $v (again, verified with od). If your shell does not work this way, then it's an egregious violation of the posix standard. It's more likely that you are doing something wrong when you are accessing/printing the variable's value:
Code:
$ printf 'a\n\n\n\n\n'
a




$ printf 'a\n\n\n\n\n' | od -c
0000000    a  \n  \n  \n  \n  \n                                        
0000006
$ v=$(printf 'a\n\n\n\n\n')
$ echo $v
a
$ printf %s "$v" | od -c
0000000    a                                                            
0000001
$ printf %s "$v"        
a$<my shell prompt here without preceding newline>

Regards,
Alister

Last edited by alister; 10-03-2010 at 07:40 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies

2. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

3. Shell Programming and Scripting

Trim whitespace and add line break

All, I'm a newbie at shell scripting and regular expressions and I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but... (7 Replies)
Discussion started by: moose1
7 Replies

4. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

5. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies

6. Shell Programming and Scripting

Trim issue

Hi All, I am using trim in my code.. ID_SA_SOURCE="`echo "$data" | cut -c17-34 | tr -s " "`" ID_SA_DEST="`echo "$data" | cut -c35-52 | tr -s " "`" echo"$ID_SA_SOURCE";"$ID_SA_DEST"; the output is 0608166896; 3001339; contains one whitespace between the two ..how can i remove that single... (3 Replies)
Discussion started by: scorpio
3 Replies

7. Shell Programming and Scripting

another trim question using tr

Hi all, I've been looking for how to eliminate blank spaces in a variable or strings. I've seen several ways, using sed, awk and even python. One of them is use 'tr' command, but it does not work as I expected: For example: echo " stuff " | tr -s " "leaves one space ahead and another... (3 Replies)
Discussion started by: AlbertGM
3 Replies

8. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

9. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

10. Shell Programming and Scripting

Trim Filename

Hi All, I have a file named as FAB1_600015_CONRAD.A0_7XYZ12345.000_LT-SWET.01_LTPA25L_20110622-161429_07_WFR12345_20110622-161429_20110712-125228.data.dis I want to generate a directory taking only the 7XYZ12345.000_WFR12345 The length and format of the Filename will be the same... (2 Replies)
Discussion started by: asheshrocky
2 Replies
dhclient-script(8)					      System Manager's Manual						dhclient-script(8)

NAME
dhclient-script - DHCP client network configuration script DESCRIPTION
The DHCP client network configuration script is invoked from time to time by dhclient(8). This script is used by the dhcp client to set each interface's initial configuration prior to requesting an address, to test the address once it has been offered, and to set the inter- face's final configuration once a lease has been acquired. If no lease is acquired, the script is used to test predefined leases, if any, and also called once if no valid lease can be identified. This script is not meant to be customized by the end user. If local customizations are needed, they should be possible using the enter and exit hooks provided (see HOOKS for details). These hooks will allow the user to override the default behaviour of the client in creating a /etc/resolv.conf file. No standard client script exists for some operating systems, even though the actual client may work, so a pioneering user may well need to create a new script or modify an existing one. In general, customizations specific to a particular computer should be done in the /etc/dhclient.conf file. If you find that you can't make such a customization without customizing /etc/dhclient.conf or using the enter and exit hooks, please submit a bug report. HOOKS
When it starts, the client script first defines a shell function, make_resolv_conf , which is later used to create the /etc/resolv.conf file. To override the default behaviour, redefine this function in the enter hook script. On after defining the make_resolv_conf function, the client script checks for the presence of an executable /etc/dhclient-enter-hooks script, and if present, it invokes the script inline, using the Bourne shell '.' command. The entire environment documented under OPERA- TION is available to this script, which may modify the environment if needed to change the behaviour of the script. If an error occurs during the execution of the script, it can set the exit_status variable to a nonzero value, and /sbin/dhclient-script will exit with that error code immediately after the client script exits. After all processing has completed, /sbin/dhclient-script checks for the presence of an executable /etc/dhclient-exit-hooks script, which if present is invoked using the '.' command. The exit status of dhclient-script will be passed to dhclient-exit-hooks in the exit_status shell variable, and will always be zero if the script succeeded at the task for which it was invoked. The rest of the environment as described previously for dhclient-enter-hooks is also present. The /etc/dhclient-exit-hooks script can modify the valid of exit_status to change the exit status of dhclient-script. OPERATION
When dhclient needs to invoke the client configuration script, it defines a set of variables in the environment, and then invokes /sbin/dhclient-script. In all cases, $reason is set to the name of the reason why the script has been invoked. The following reasons are currently defined: MEDIUM, PREINIT, BOUND, RENEW, REBIND, REBOOT, EXPIRE, FAIL and TIMEOUT. MEDIUM
The DHCP client is requesting that an interface's media type be set. The interface name is passed in $interface, and the media type is passed in $medium. PREINIT
The DHCP client is requesting that an interface be configured as required in order to send packets prior to receiving an actual address. For clients which use the BSD socket library, this means configuring the interface with an IP address of 0.0.0.0 and a broadcast address of 255.255.255.255. For other clients, it may be possible to simply configure the interface up without actually giving it an IP address at all. The interface name is passed in $interface, and the media type in $medium. If an IP alias has been declared in dhclient.conf, its address will be passed in $alias_ip_address, and that ip alias should be deleted from the interface, along with any routes to it. BOUND
The DHCP client has done an initial binding to a new address. The new ip address is passed in $new_ip_address, and the interface name is passed in $interface. The media type is passed in $medium. Any options acquired from the server are passed using the option name described in dhcp-options, except that dashes ('-') are replaced by underscores ('_') in order to make valid shell variables, and the vari- able names start with new_. So for example, the new subnet mask would be passed in $new_subnet_mask. Before actually configuring the address, dhclient-script should somehow ARP for it and exit with a nonzero status if it receives a reply. In this case, the client will send a DHCPDECLINE message to the server and acquire a different address. This may also be done in the RENEW, REBIND, or REBOOT states, but is not required, and indeed may not be desirable. When a binding has been completed, a lot of network parameters are likely to need to be set up. A new /etc/resolv.conf needs to be cre- ated, using the values of $new_domain_name and $new_domain_name_servers (which may list more than one server, seperated by spaces). A default route should be set using $new_routers, and static routes may need to be set up using $new_static_routes. If an IP alias has been declared, it must be set up here. The alias IP address will be written as $alias_ip_address, and other DHCP options that are set for the alias (e.g., subnet mask) will be passed in variables named as described previously except starting with $alias_ instead of $new_. Care should be taken that the alias IP address not be used if it is identical to the bound IP address ($new_ip_address), since the other alias parameters may be incorrect in this case. RENEW
When a binding has been renewed, the script is called as in BOUND, except that in addition to all the variables starting with $new_, there is another set of variables starting with $old_. Persistent settings that may have changed need to be deleted - for example, if a local route to the bound address is being configured, the old local route should be deleted. If the default route has changed, the old default route should be deleted. If the static routes have changed, the old ones should be deleted. Otherwise, processing can be done as with BOUND. REBIND
The DHCP client has rebound to a new DHCP server. This can be handled as with RENEW, except that if the IP address has changed, the ARP table should be cleared. REBOOT
The DHCP client has successfully reacquired its old address after a reboot. This can be processed as with BOUND. EXPIRE
The DHCP client has failed to renew its lease or acquire a new one, and the lease has expired. The IP address must be relinquished, and all related parameters should be deleted, as in RENEW and REBIND. FAIL
The DHCP client has been unable to contact any DHCP servers, and any leases that have been tested have not proved to be valid. The param- eters from the last lease tested should be deconfigured. This can be handled in the same way as EXPIRE. TIMEOUT
The DHCP client has been unable to contact any DHCP servers. However, an old lease has been identified, and its parameters have been passed in as with BOUND. The client configuration script should test these parameters and, if it has reason to believe they are valid, should exit with a value of zero. If not, it should exit with a nonzero value. The usual way to test a lease is to set up the network as with REBIND (since this may be called to test more than one lease) and then ping the first router defined in $routers. If a response is received, the lease must be valid for the network to which the interface is cur- rently connected. It would be more complete to try to ping all of the routers listed in $new_routers, as well as those listed in $new_static_routes, but current scripts do not do this. FILES
Each operating system should generally have its own script file, although the script files for similar operating systems may be similar or even identical. The script files included in the Internet Software Consortium DHCP distribution appear in the distribution tree under client/scripts, and bear the names of the operating systems on which they are intended to work. BUGS
If more than one interface is being used, there's no obvious way to avoid clashes between server-supplied configuration parameters - for example, the stock dhclient-script rewrites /etc/resolv.conf. If more than one interface is being configured, /etc/resolv.conf will be repeatedly initialized to the values provided by one server, and then the other. Assuming the information provided by both servers is valid, this shouldn't cause any real problems, but it could be confusing. SEE ALSO
dhclient(8), dhcpd(8), dhcrelay(8), dhclient.conf(5) and dhclient.leases(5). AUTHOR
dhclient-script(8) has been written for the Internet Software Consortium by Ted Lemon in cooperation with Vixie Enterprises. To learn more about the Internet Software Consortium, see http://www.isc.org. To learn more about Vixie Enterprises, see http://www.vix.com. dhclient-script(8)
All times are GMT -4. The time now is 03:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy