EzDevInfo.com

linux interview questions

Top linux frequently asked interview questions

How can I redirect and append both stdout and stderr to a file with Bash?

To redirect stdout to a truncated file in Bash, I know to use:

cmd > file.txt

To redirect stdout in Bash, appending to a file, I know to use:

cmd >> file.txt

To redirect both stdout and stderr to a truncated file, I know to use:

cmd &> file.txt

How do I redirect both stdout and stderr appending to a file? cmd &>> file.txt did not work for me.


Source: (StackOverflow)

UTF-8 all the way through

I'm setting up a new server, and want to support UTF-8 fully in my web application. I have tried in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.

Where exactly do I need to set the encoding/charsets? I'm aware that I need to configure Apache, MySQL and PHP to do this - is there some standard checklist I can follow, or perhaps troubleshoot where the mismatches occur?

This is for a new Linux server, running MySQL 5, PHP 5 and Apache 2.


Source: (StackOverflow)

Advertisements

How to set a variable equal to the output from a command in Bash?

I am working on a simple scripting project for work that involves the use of Bash. I have a pretty simple script that is something like the following:

#!/bin/bash

VAR1="$1"
VAR2="$2"

MOREF='sudo run command against $VAR1 | grep name | cut -c7-'

echo $MOREF

When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF variable, I am able to get output. I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?


Source: (StackOverflow)

How to symlink a file in Linux?

I want to make a symbolic link in Linux. I have written this bash command where the first path is the folder I want link into and the second path is the compiled source.

ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal 

Is this correct?


Source: (StackOverflow)

Finding all files containing a text string on Linux

I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text. Just to clarify, I'm looking for text within the file, not in the file name.

When I was looking up how to do this, I came across this solution twice:

find / -type f -exec grep -H 'text-to-find-here' {} \;

However, it doesn't work. It seems to display every single file in the system.

Is this close to the proper way to do it? If not, how should I? This ability to find text strings in files would be extraordinary useful for me for some programming projects I'm doing.


Source: (StackOverflow)

Why does the C preprocessor interpret the word "linux" as the constant "1"?

Why does the C preprocessor in GCC interpret the word linux (small letters) as the constant 1?

test.c:

#include <stdio.h>
int main(void)
{       
    int linux = 5;
    return 0;
}

Result of $ gcc -E test.c (stop after the preprocessing stage):

....
int main(void)
{
    int 1 = 5;
    return 0;
}

Which -of course- yields an error.

(BTW: There is no #define linux in the stdio.h file.)


Source: (StackOverflow)

Exclude directory from find . command

I'm trying to run a find command for all JavaScript files, but how do I exclude a specific directory?

Here is the find code we're using.

for file in $(find . -name '*.js'); do java -jar config/yuicompressor-2.4.2.jar --type js $file -o $file; done

Source: (StackOverflow)

Pipe to/from Clipboard

Is it possible to pipe to/from the clipboard in bash? Whether it is piping to/from a device handle or using an auxiliary application, I can't find anything.

For example, if /dev/clip was a device linking to the clipboard we could do:

cat /dev/clip        # dump the contents of the clipboard
cat foo > /dev/clip  # dump the contents of "foo" into the clipboard

Source: (StackOverflow)

How to download a file from server using SSH? [closed]

Need to download on my desktop a file from server. (UBUNTU 10.04) I haven't web access to server, just ssh.

If its help, my OS is Mac OS X and iTerm 2 as a terminal.


Source: (StackOverflow)

Redirect all output to file

I know that in Linux, to redirect output from the screen to a file, I can either use the > or tee. However, I'm not sure why part of the output is still output to the screen and not written to the file.

Is there a way to redirect all output to file?


Source: (StackOverflow)

Convert DOS line endings to Linux line endings in vim

If I open files I created in Windows, the lines all end with ^M.
How do I delete them all in once?


Source: (StackOverflow)

Starting iPhone app development in Linux? [closed]

I've heard that you need to get a Mac if you want to develop iPhone apps. Is this true?

Is it possible to develop iPhone apps using Linux? If yes, what do I need and where do I download the necessary tools?


Source: (StackOverflow)

How can I recursively find all files in current and subfolders based on wildcard matching? [closed]

How can I recursively find all files in current and subfolders based on wildcard matching?


Source: (StackOverflow)

Looping through the content of a file in Bash?

How do I iterate through each line of a text file with Bash?

With this script

echo "Start!"
for p in (peptides.txt)
do
    echo "${p}"
done

I get this output on the screen:

Start!
./runPep.sh: line 3: syntax error near unexpected token `('
./runPep.sh: line 3: `for p in (peptides.txt)'

(Later I want to do something more complicated with $p than just output to the screen.)


The environment variable SHELL is (from env):

SHELL=/bin/bash

/bin/bash --version output:

GNU bash, version 3.1.17(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

cat /proc/version output:

Linux version 2.6.18.2-34-default (geeko@buildhost) (gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Mon Nov 27 11:46:27 UTC 2006

The file peptides.txt contains:

RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL

Source: (StackOverflow)

Kill detached screen session [closed]

I learned from somewhere a detached screen can be killed by

screen -X -S [session # you want to kill] kill

where [session # you want to kill] can be gotten from

screen -ls .

But this doesn't work. Anything wrong? What's the correct way?


Source: (StackOverflow)