EzDevInfo.com

shell-script interview questions

Top shell-script frequently asked interview questions

How to replace part of a text file between markers with another text file?

Say I have a text file like this:

# custom content section
a
b

### BEGIN GENERATED CONTENT
c
d
### END GENERATED CONTENT

I'd like to replace the portion between the GENERATED CONTENT tags with the contents of another file.

What's the simplest way to do this?


Source: (StackOverflow)

Who deals with the star * in echo *

Who deals (interprets) the * in

echo *

Does the echo see the star or the shell take care about it and return a list of filename ..

What about

cp temp temp*

Source: (StackOverflow)

Advertisements

How do I launch a bash script from Finder in OS X?

How do I launch a bash script from Finder in OS X?

I'm OK working at the CLI, but my little script is not so loved by my GUI-bound colleagues.


Source: (StackOverflow)

How to execute .sh file on Windows?

When I am trying to execute a file(name.sh) in the command line by the command ./name.sh , I am getting the error that:

"." is not recognized as an internal or external command, operable or batch file

please help me execute the .sh file


Source: (StackOverflow)

How do you escape apostrophe in single quoted string in bash?

I don't understand how bash evaluates escaping of apostrophe characters in single quoted strings.

Here is an example:

$ echo ''\''Hello World'\'''
'Hello World' # works

$ echo '\'Hello World\''
 > # expects you to continue input

I've tried looking for explanations to this but couldn't get anything. What is bash doing here?


Source: (StackOverflow)

Populating Array in DOS Batch Script

How can I setup an array variable in a DOS batch script? I would like to load it with a list of file names to process. I really would like to keep this as simple as possible. Thank you.


Source: (StackOverflow)

How can I run a shell script in Windows?

I'm a Mac user and new to Windows. How can I execute a shell script in Windows?

Generally, I would save the text file with the extension .sh and run it via Terminal. But how could I do that in Windows?


Source: (StackOverflow)

Avoid unwanted path in Zip file

I'm making a shell script to package some files. I'm zipping a directory like this:

zip -r /Users/me/development/something/out.zip /Users/me/development/something/folder/

The problem is that the resultant out.zip archive has the entire file path in it. That is, when unzipped, it will have the whole "/Users/me/development/anotherthing/" path in it. Is it possible to avoid these deep paths when putting a directory into an archive?

When I run zip from inside the target directory, I don't have this problem.

zip -r out.zip ./folder/

In this case, I don't get all the junk. However, the script in question will be called from wherever.

FWIW, I'm using bash on Mac OS X 10.6.


Source: (StackOverflow)

Determine the architecture of a Mac from the command line or script?

I'm writing a shell script, and I need to know the architecture, i.e. PPC or Intel. Back in the day, there was a program /bin/arch that told you, but my mac doesn't seem to have it... Is there an easy way I can do this? Grep for something in a logfile? call some other program that spits that out as a side effect?

It would be nice to know what OS Version I'm running too, but that may not be necessary.

thanks


Source: (StackOverflow)

Should I put *.sh and *.rb file extensions on all my scripts?

I have a bunch of hand-rolled executable scripts in my $HOME/bin directory. Some are written in bash, some in Ruby. All of them have the shebang line at the top telling the shell what interpreter to use (bash or Ruby, in my case).

I'm wondering if it is better to put file extensions on these scripts to indicate what scripting language they are written in? For example, the scripts in Ruby would have the *.rb suffix, and the bash ones would have the *.sh suffix.

Currently, these scripts just have simple names with no file extension.

What's the best practice?


Source: (StackOverflow)

How to extract a complete list of extension types within a directory?

Within a directory, and recursively within it's sub-directories, meaning every directory within a directory is processed, how do I compile a complete list of unique extensions within the directory?

OS is Windows XP with all the current updates, but I okay running script if I'm able to tell what it's doing, though I would prefer not to have to install dot-net, since I really do not like it.


Source: (StackOverflow)

looping through `ls` results in bash shell script

Does any one have a template shell script for doing something with ls for a list of directory names and looping through each one and doing something?

I'm planning to do ls -1d */ to get the list of directory names.


Source: (StackOverflow)

Programmatically open tab in gnome-terminal, execute command, and have tab stay open

I am trying to write a command that will launch a few terminal tabs, execute something in each tab, and have each tab stay open after the command in finished, so I can look at the output and type more commands in each tab

something like this:

gnome-terminal --tab -e "ls -a" --tab -e "ls"

but the problem with this is that the tabs close as soon as the "ls" commands finish. Does anyone know how to make the tabs stay open?


Source: (StackOverflow)

What does export do in BASH? [duplicate]

Possible Duplicate:
Difference between “a=b” and “export a=b” in bash

It is hard to admit, but I have never really understood what exactly export does to an environment variable. I know that if I don't export a variable I sometimes can't see it in child processes, but sometimes it seems like I can. What is really going on when I say

export foo=5

and when should I not export a variable?


Source: (StackOverflow)

Batch converting PNG to JPG in linux

Does anyone know a good way to batch-convert a bunch of PNGs into JPGs in linux? (I'm using Ubuntu).

A png2jpg binary that I could just drop into a shell script would be ideal.


Source: (StackOverflow)