EzDevInfo.com

shell interview questions

Top shell frequently asked interview questions

How can I replace a newline (\n) using sed?

How can I replace a newline (\n) using sed?

I unsuccesfully tried:

sed 's#\n# #g' file
sed 's#^$# #g' file

How to fix it?


Source: (StackOverflow)

How to mkdir only if a dir does not already exist?

I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exists" error that mkdir throws when it tries to create an existing directory.

Any thoughts on how best to do this?


Source: (StackOverflow)

Advertisements

How to count all the lines of code in a directory recursively?

We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories. We don't need to ignore comments, as we're just trying to get a rough idea.

wc -l *.php

That command works great within a given directory, but ignores subdirectories. I was thinking this might work, but it is returning 74, which is definitely not the case...

find . -name '*.php' | wc -l

What's the correct syntax to feed in all the files?


Source: (StackOverflow)

How to reload .bash_profile from the command line?

How can I reload .bash_profile from the command line? I can get the shell to recognize changes to .bash_profile by exiting and logging back in but I would like to be able to do it on demand.


Source: (StackOverflow)

Get current directory name (without full path) in Bash Script

How would I get just the current working directory name in a bash script, or even better, just a terminal command.

pwd gives the full path of the current working directory, e.g. /opt/local/bin but I only want bin


Source: (StackOverflow)

How can I concatenate string variables in Bash?

In PHP I would add strings together like this:

$foo = "Hello";
$foo .= " World";

So $foo would be "Hello World"

How would I do that in Bash?


Source: (StackOverflow)

How to determine whether a given Linux is 32 bit or 64 bit?

When I type uname -a, it gives the following output.

Linux mars 2.6.9-67.0.15.ELsmp #1 SMP Tue Apr 22 13:50:33 EDT 2008 i686 i686 i386 GNU/Linux

How can I know from this that the given OS is 32 or 64 bit?

This is useful when writing configure scripts, for example: what architecture am I building for?


Source: (StackOverflow)

How do I prompt for input in a Linux shell script?

I want to pause input in a shell script, and prompt the user for choices. The standard 'Yes, No, or Cancel' type question. How do I accomplish this in a typical bash prompt?


Source: (StackOverflow)

Count number of lines in a git repository

How would I count the total number of lines present in all the files in a git repository?

git ls-files gives me a list of files tracked by git.

I'm looking for a command to cat all those files. Something like

git ls-files | [cat all these files] | wc -l

Source: (StackOverflow)

how to use ssh to run shell script on a remote machine? [closed]

Could you please suggest me how to run a shell script on remote machine?

I have ssh configured on both machine A and B. My script is on machine A which will perform a task on machine B.


Source: (StackOverflow)

Calling shell commands from Ruby

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?


Source: (StackOverflow)

How do I iterate over a range of numbers defined by variables in bash?

How do I iterate over a range of numbers in bash when the range is given by a variable?

I know I can do this (called "sequence expression" in the bash documentation):

 for i in {1..5}; do echo $i; done

Which gives:

1
2
3
4
5

Yet how can I replace either of the range endpoints with a variable? This doesn't work:

END=5
for i in {1..$END}; do echo $i; done

Which prints:

{1..5}


Source: (StackOverflow)

Difference between sh and bash

When writing shell programs, we often use /bin/sh and /bin/bash. I usually use bash, but I don't know what's the difference between them.

What's main difference between bash and sh?

What do we need to be aware of when programming in bash and sh?


Source: (StackOverflow)

Defining a variable with or without export

What is export for?

What is the difference between:

export name=value

and

name=value

Source: (StackOverflow)

Check existence of input argument in a Bash shell script

I need to check the existence of an input argument. I have the following script:

if [ "$1" -gt "-1" ]
  then echo hi
fi

I get

[: : integer expression expected

How do I check the input argument1 first to see if it exists?


Source: (StackOverflow)