EzDevInfo.com

sh

Python process launching sh 1.11 — sh 1.11 documentation

'echo' without newline in a shell script

I have a problem with echo in my script:

echo -n "Some string..."

prints

-n Some string...

and moves to the next line. In the console it's working correcly without newline:

Some string...

Source: (StackOverflow)

How to execute mongo commands through shell scripts?

I want to execute mongo commands in shell script.

I tried following way test.sh

#!/bin/sh

mongo myDbName

db.mycollection.findOne()

show collections

When I execute above script ./test.sh

Then mongo connection established but next commands not executed

How to execute other commands through sh script [test.sh] ?

Please help me


Source: (StackOverflow)

Advertisements

Git Alias - Multiple Commands and Parameters

I am trying to create an alias that uses both multiple git commands and positional parameters. There are stackoverflow pages for each, and it would appear painfully obvious to do both, but I am having trouble.

As an example, I want to switch to branch foo and perform a status. So in my .gitconfig, I have:

  [alias] 
     chs = !sh -c 'git checkout $0 && git status'

which doesn't work. Whereas something like this will work.

chs = !sh -c 'git checkout $0'

echoes = !sh -c 'echo hi && echo bye'

Any insight would be appreciated.

Thanks!


Source: (StackOverflow)

BASH ^word^replacement^ on all matches?

To clarify, I am looking for a way to perform a global search and replace on the previous command used. ^word^replacement^ only seems to replace the first match.

Is there some set option that is eluding me?


Source: (StackOverflow)

How to run a shell script in the background and get no output

I wrote two shell scripts a.sh and b.sh. In a.sh and b.sh I have a infinite for loop and they print some output to the terminal. I want to write another script which calls both a.sh and b.sh but I want the user to regain control of the terminal immediately, instead of having the script run infinitely and I want to hide the output in terminal.


Source: (StackOverflow)

How can I tell if a file is older than 30 minutes from /bin/sh?

How do I write a script to determine if a file is older than 30 minutes in /bin/sh?

Unfortunately does not stat exist in the system. It is an old Unix system, http://en.wikipedia.org/wiki/Interactive_Unix

Perl is unfortunately not installed on the system and the customer does not want to install it, and nothing else either.


Source: (StackOverflow)

How to declare and use boolean variables in shell script?

I tried to declare a boolean variable in a shell script using the following syntax:

variable=$false

variable=$true

Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using boolean variables as expressions correct:

if [ $variable ]

if [ !$variable ]

Source: (StackOverflow)

Bash script variable declaration - command not found

This seems like such a simple question I'm embarrassed to ask it:

test.sh

#!/bin/bash
STR = "Hello World"
echo $STR

when I run sh test.sh I get this:

test.sh: line 2: STR: command not found

What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and this is how they say to declare variables... So I'm not sure what I'm doing wrong.

I'm on Ubuntu Server 9.10. And yes, bash is located at /bin/bash.


Source: (StackOverflow)

How can I get the behavior of GNU's readlink -f on a Mac?

On Linux, the readlink utility accepts an option -f that follows additional links. This doesn't seem to work on Mac and possibly BSD based systems. What would the equivalent be?

Here's some debug information:

$ which readlink; readlink -f
/usr/bin/readlink
readlink: illegal option -f
usage: readlink [-n] [file ...]

Source: (StackOverflow)

How to get absolute path name of shell script on MacOS?

readlink -f does not exist on MacOS. The only working solution for Mac OS I managed to find on the net goes like this:

if [[ $(echo $0 | awk '/^\//') == $0 ]]; then
    ABSPATH=$(dirname $0)
else
    ABSPATH=$PWD/$(dirname $0)
fi

Can anyone suggest anything more elegant to this seemingly trivial task?


Source: (StackOverflow)

Deleting lines from one file which are in another file

I have a file f1:

line1
line2
line3
line4
..
..

I want to delete all the lines which are in another file f2:

line2
line8
..
..

I tried something with cat and sed, which wasn't even close to what I intended. How can I do this?


Source: (StackOverflow)

Shell Programming: What's the difference between $(command) and `command`

In sh/ksh/bash to store the output of a command as a variable you can do either

MY_VAR=$(command)
#or you can do
MY_VAR=`command`

What's the difference if any between the two methods?


Source: (StackOverflow)

How do I merge one directory into another using Bash?

I'm looking for shell script that merge files from one directory into another.

Sample:

html/
  a/
    b.html
  index.html

html_new/
  a/
    b2.html
    b.html

Usage:

./mergedirs.sh html html_new

Result:

html/
  a/
    b.html
    b2.html
  index.html

html/a/b.html was replaced by html_new/a/b.html
html/a/b2.html was copied to html_new/a/b2.html
html/index.html kept untouched


Source: (StackOverflow)

What is sudo bang bang? [closed]

make me a sandwich, sudo bang bang

Besides from this comic, I have seen some people write sudo !!.

What is sudo bang bang?


Source: (StackOverflow)

What does the line "#!/bin/sh" mean in a UNIX shell script?

I was going through some shell script tutorials and found the following sample program:

#!/bin/sh
clear
echo "HELLO WORLD"

Can anyone please tell what is the significance of mentioning '!/bin/sh' in the comment?

Thanks.


Source: (StackOverflow)