Tuesday, November 18, 2014

Linux: List All Environment Variables Command


How do I display all my environment variables using bash shell on RHEL / Debian / Ubuntu / CentOS / Fedora / Mint Linux operating systems?

You can use any one of the following command to display the environment variables and their values.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time1m
a) printenv command - Print all or part of environment.
b) env command - Print all exported environment or run a program in a modified environment.
c) set command - Print the name and value of each shell variable.

Examples

I recommend that you use the printenv command:
printenv
OR
printenv | less
OR
printenv | more
Sample outputs:
Fig.01: Command to see a list of all currently defined environment variables in a Linux bash terminal
Fig.01: Command to see a list of all currently defined environment variables in a Linux bash terminal

A list of the commonly used variables in Linux

System VariableMeaningTo View Variable Value Type
BASH_VERSIONHolds the version of this instance of bash.echo $BASH_VERSION
HOSTNAMEThe name of the your computer.echo $HOSTNAME
CDPATHThe search path for the cd command.echo $CDPATH
HISTFILEThe name of the file in which command history is saved.echo $HISTFILE
HISTFILESIZEThe maximum number of lines contained in the history file.echo $HISTFILESIZE
HISTSIZEThe number of commands to remember in the command history. The default value is 500.echo $HISTSIZE
HOMEThe home directory of the current user.echo $HOME
IFSThe Internal Field Separator that is used for word splitting after expansion and to split lines into words with
the read builtin command. The default value is <space><tab><newline>.
echo $IFS
LANGUsed to determine the locale category for any category not specifically selected with a variable starting with LC_.echo $LANG
PATHThe search path for commands. It is a colon-separated list of directories in which the shell looks for commands.echo $PATH
PS1Your prompt settings.echo $PS1
TMOUTThe default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as
the number of seconds to wait for input after issuing the command. If not input provided it will logout user.
echo $TMOUT
TERMYour login terminal type.echo $TERM
export TERM=vt100
SHELLSet path to login shell.echo $SHELL
DISPLAYSet X display nameecho $DISPLAY
export DISPLAY=:0.1
EDITORSet name of default text editor.export EDITOR=/usr/bin/vim

set and env command

You can use the env / set command too:
 
env
env | more
set
set | more
 
Sample outputs:
HOME=/home/vivek
vivek@nas01:~$ env
TERM=xterm-256color
SHELL=/bin/bash
XDG_SESSION_COOKIE=9ee90112ba2cb349f07bfe2f00002e46-1381581541.324726-906214463
SSH_CLIENT=192.168.1.6 60190 22
SSH_TTY=/dev/pts/1
USER=vivek
MAIL=/var/mail/vivek
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
PWD=/home/vivek
LANG=en_IN
SHLVL=1
HOME=/home/vivek
LANGUAGE=en_IN:en
LOGNAME=vivek
SSH_CONNECTION=192.168.1.6 60190 192.168.1.10 22
_=/usr/bin/env

A note about env/set command

The env will only display a list of environment variables that have been exported and it will not show all bash variables. The set command allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. If no options or arguments are supplied, set displays the names and values of all shell variables and functions, sorted according to the current locale, in a format that may be reused as input for setting or resetting the currently-set variables. Hence, I recommend that you use printenv command to dump the list of all shell variables on screen. To save the list of all shell environment variables to a file, enter:
 
printenv > env.txt
cat env.txt
 
Use the grep command to search for particular variable:
 
printenv | grep foo
printenv | grep HOME
 
RECOMMEND READINGS
ref: http://www.cyberciti.biz/faq/linux-list-all-environment-variables-env-command/


Monday, November 17, 2014

replaceAll vào String object

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
){
var strText = this;
var intIndexOfMatch = strText.indexOf( strTarget );
 
// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strText = strText.replace( strTarget, strSubString )
 
// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf( strTarget );
}
 
// Return the updated string with ALL the target strings
// replaced out with the new substring.
return( strText );
}





1
alert(myString.replaceAll('my', 'his')); //output : "his blog of his friend here".


Sunday, November 16, 2014

how to get ServletContext from jsp

JSPs in Weblogic behaves differently from Tomcat with respect to getServletContext
ServletContext sc = getServletConfig().getServletContext();

Saturday, November 15, 2014

javascript-error-uncaught-syntaxerror-unexpected-end-of-input

Add a second });.
When properly indented, your code reads
$(function() {
    $("#mewlyDiagnosed").hover(function() {
        $("#mewlyDiagnosed").animate({'height': '237px', 'top': "-75px"});
    }, function() {
        $("#mewlyDiagnosed").animate({'height': '162px', 'top': "0px"});
    });
MISSING!
You never closed the outer $(function() {.


Spring MVC ajax request with UTF 8 support

Recently for my Dropbox Writer project, I ran into a issue with transferring none English characters between server and client side. There are not a lot of solutions on the web and those solutions are very inconsistent.

Explain my situation

I created a Spring MVC project what can create text files in Dropbox. Dropbox uses UTF 8 for text encoding, thus it supports non English characters. My application uses ajax to drive rest service calls to transfer data between html based client side and spring mvc based server side. I’ve added CharacterEncodingFilter to the web.xml as following:
<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

The problem

When I create a file with name contains Chinese characters, the returned file name is scrambled. The problem is my application uses @ResponseBody in my restful ajax calls to produce json data but those data is not returned in UTF 8 format.
I’ve spent sometime researching and testing a few solutions and I want to share those with you.

Solution 1

Don’t use @ResponseBody. Return void in the controller, and use HtppServletResponse to return string with valid formatting.
@RequestMapping(value = "/rest/create/document")
public void create(Document document, HttpServletRespone respone) throws UnsupportedEncodingException {
 
    Document newDocument = DocumentService.create(Document);
 
    String json = jsonSerializer.serialize(newDocument);
 
    OutputStream outputStream = respones.getOutputStream();
 
    outputStream.write(json.getBytes("UTF-8"));
 
    outputStream.close();
}
Advantage: This will always output text with correct text format, and it’s very simple to understand.
Disadvantage: Very cumbersome. You are going away from the Spring MVC model and writing directly to the HttpServlet model.

Solution 2

Return the an Response Entity object and set it’s content type.
@RequestMapping(value = "/rest/create/document")
public ResponseEntity&lt;String&gt; create(Document document, HttpServletRespone respone) 
 
   HttpHeaders responseHeaders = new HttpHeaders();
 
 responseHeaders.add("Content-Type", "text/html; charset=utf-8");
 
 Document newDocument = DocumentService.create(Document);
 
 String json = jsonSerializer.serialize(newDocument);
 
 return new ResponseEntity&lt;String&gt;(json, responseHeaders, HttpStatus.OK);
}
Better then the last solution, but you still feel there is a lot of plumbing involved.

Solution 3

This is it; this is the most elegant solution and it took me a while to figure out.
@RequestMapping(value = "/rest/create/document", produces = "text/plain;charset=UTF-8")
@ResponseBody
public String create(Document document, HttpServletRespone respone) throws UnsupportedEncodingException {
 
    Document newDocument = DocumentService.create(Document);
 
    return jsonSerializer.serialize(newDocument);
}
When I saw this solution, it blown my mind. Yes this is really that simple. There is no plumbing, you tell spring mvc directly how to return formatted text. The downside is you will always need to return UTF 8 text.

json spring mvc java

@RequestMapping(value = "/rest/create/document", produces = "text/plain;charset=UTF-8")
@ResponseBody
public String create(Document document, HttpServletRespone respone) throws UnsupportedEncodingException {

    Document newDocument = DocumentService.create(Document);

    return jsonSerializer.serialize(newDocument);
}

<mvc:annotation-driven>
  <mvc:message-converters register-defaults="true">
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
      <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
    </bean>
  </mvc:message-converters>
</mvc:annotation-driven>
Or code-based configuration:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  private static final Charset UTF8 = Charset.forName("UTF-8");

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", UTF8)));
    converters.add(stringConverter);

    // Add other converters ...
  }
}