Infrastructure Management Shell - Linux Netkiller ebook - Linux ebook

Check 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

I get

[: : integer expression expected

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

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)

Defining a variable with or without export

What is export for?

What is the difference between:

export name=value



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)

How to set a BASH variable equal to the output from a command?

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:



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 able to get 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)

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)

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)

Shell command to sum integers, one per line?

I am looking for a command that will accept as input multiple lines of text, each line containing a single integer, and output the sum of these integers.

As a bit of background, I have a log file which includes timing measurements, so through grepping for the relevant lines, and a bit of sed reformatting I can list all of the timings in that file. I'd like to work out the total however, and my mind has gone blank as to any command I can pipe this intermediate output to in order to do the final sum. I've always used expr in the past, but unless it runs in RPN mode I don't think it's going to cope with this (and even then it would be tricky).

What am I missing? Given that there are probably several ways to achieve this, I will be happy to read (and upvote) any approach that works, even if someone else has already posted a different solution that does the job.

Related question: Shortest command to calculate the sum of a column of output on Unix? (credits @Andrew)

Update: Wow, as expected there are some nice answers here. Looks like I will definitely have to give awk deeper inspection as a command-line tool in general!

Source: (StackOverflow)

How to check if a variable is set in Bash?

How do I know if a variable is set in Bash?

For example, how do I check if the user gave the first parameter to a function?

function a {
    ?? if $1 is set

Source: (StackOverflow)

Use grep --exclude/--include syntax to not grep through certain files

I'm looking for the string "foo=" (without quotes) in text files in a directory tree. It's on a common Linux machine, I have bash shell:

grep -ircl "foo=" *

In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that?

I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format? The man page of grep says:

--include=PATTERN     Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN     Recurse in directories skip file matching PATTERN.

Searching on grep include, grep include exclude, grep exclude and variants did not find anything relevant

If there's a better way of grepping only in certain files, I'm all for it; moving the offending files is not an option. I can't search only certain directories (the directory structure is a big mess, with everything everywhere). Also, I can't install anything, so I have to do with common tools (like grep or the suggested find).

UPDATES: @Adam Rosenfield's answer is just what I was looking for:

grep -ircl --exclude=\*.{png,jpg} "foo=" *

@rmeador's answer is also a good solution:

grep -Ir --exclude="*\.svn*" "pattern" *

It searches recursively, ignores binary files, and doesn't look inside Subversion hidden folders.(...)

Source: (StackOverflow)

Converting string to lower case in Bash shell scripting

Is there a way in Bash shell scripting so that I can convert a string into lower case string?

For example,

if $a = "Hi all"

I want to convert it to

$a = "hi all"

Source: (StackOverflow)

How do I put an already-running process under nohup?

I have a process that is already running for a long time and don't want to end it.

How do I put it under nohup (that is, how do I cause it to continue running even if I close the terminal?)

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:


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

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

Which prints:


Source: (StackOverflow)