Archive

Archive for the ‘bash’ Category

GNU Makefile setting env variables

January 12, 2019 Leave a comment

Problem:
Exporting env variables in Makefiles. The issue was that the environment variables were not visible across different targets. I needed the same env vars to be seen in both target1 and target2 below.

Solution:
Export the variables outside the target, eg at the top of the file.

VALUE1="myvalue1"
VALUE2="myvalue2"

export VALUE1
export VALUE2

target1:
    echo $VALUE1

target2:
    echo $VALUE2

Source:
https://stackoverflow.com/questions/2826029/passing-additional-variables-from-command-line-to-make
https://www.linuxquestions.org/questions/programming-9/exporting-makefile-variables-to-%24-shell-environment-807422/

Advertisements
Categories: bash, Interesting Tags: ,

Useful Linux Command Line Bash Shortcuts You Should Know

November 21, 2018 Leave a comment

In this article, we will share a number of Bash command-line shortcuts useful for any Linux user. These shortcuts allow you to easily and in a fast manner, perform certain activities such as accessing and running previously executed commands, opening an editor, editing/deleting/changing text on the command line, moving the cursor, controlling processes etc. on the command line.

Although this article will mostly benefit Linux beginners getting their way around with command line basics, those with intermediate skills and advanced users might also find it practically helpful. We will group the bash keyboard shortcuts according to categories as follows.

https://www.tecmint.com/linux-command-line-bash-shortcut-keys/

Categories: bash, Interesting

docker: Error response from daemon…

August 22, 2018 Leave a comment

Problem:
I was getting this error.

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused “exec: \”telnet\”: executable file not found in $PATH”: unknown.

$ docker run --name greeter -d --expose 5000 alpine /bin/sh -c "echo Welcome stranger | nc -lp 5000"
51b6a0b73dc1cbc9b39757dff20ada1f5638c1ec781625d9e2fc6c9cced01639
$ docker exec greeter ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:758 (758.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
$ docker run alpine telnet 172.17.0.2 5000
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"telnet\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled
$ docker run -it alpine /bin/sh
/ # telnet 172.17.0.2 5000
/bin/sh: telnet: not found

Solution:
This error means that whatever I am trying to run does not exist. So my options are to install telnet in the container or use a different command like `nc`.

Categories: bash Tags:

port forwarding

August 17, 2018 Leave a comment

Problem:

You want to access a site with a particular IP. Eg google maps only allows a set of IPs to access. So running code from a vagrant box then accessing from the host is not so straight forward.

Solution:
Went with using ssh

$ ssh -nNT -L 3000:localhost:3000 vagrant@127.0.0.1 -p 2222

So on the host I can access a service running on a vagrant box (192.168.33.10:3000) via localhost:3000

Categories: bash

find command examples

October 24, 2017 Leave a comment

Problem:
I needed to clean up after running tests

Solution:
Using find

$ tree
.
├── bin
│   └── hello
├── hello
│   ├── hello.py
│   └── __init__.py
├── requirements.txt
├── setup.py
└── tests
    ├── __init__.py
    └── test_hello.py

3 directories, 7 files
$ python3 setup.py test                                            
running test
running egg_info
creating hello.egg-info
writing dependency_links to hello.egg-info/dependency_links.txt
writing top-level names to hello.egg-info/top_level.txt
writing hello.egg-info/PKG-INFO
writing manifest file 'hello.egg-info/SOURCES.txt'
reading manifest file 'hello.egg-info/SOURCES.txt'
writing manifest file 'hello.egg-info/SOURCES.txt'
running build_ext
test_print_hello (tests.test_hello.TestHello) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.014s

OK

Now more files are created.

$ tree
.
├── bin
│   └── hello
├── hello
│   ├── hello.py
│   ├── __init__.py
│   └── __pycache__
│       ├── hello.cpython-35.pyc
│       └── __init__.cpython-35.pyc
├── hello.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── requirements.txt
├── setup.py
└── tests
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-35.pyc
    │   └── test_hello.cpython-35.pyc
    └── test_hello.py

6 directories, 15 files

To delete the *.pyc files and the __pycache__ and hello.egg-info folders, run the following commands.

$ find . -type d -name "hello.egg-info" | xargs rm -rf
$ find . -type d -name "__pycache__" | xargs rm -rf
$ find . -name "*.pyc" | xargs rm -f

Note:

$ find --version                                                   
find (GNU findutils) 4.7.0-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2) 

Source:
https://superuser.com/questions/736272/using-unixs-find-command-to-find-directories-matching-name-but-not-subdirectori#736275

Categories: bash Tags: ,

upstart

August 1, 2017 Leave a comment

Problem:
Well this was more like an irritant. I have a portforwarding script that I manually start. I just got tired of doing that and moved it to an upstart task.

Solution:
Added the following job for upstart to handle.

$ less /etc/init/portforward.conf
description     "Portforward 3141 to devpi server"

author      "Vagrant"

start on runlevel [2345]
stop on runlevel [!2345]

script
    cd /home/vagrant

    export SSHPASS="vagrant"
    sshpass -e ssh -nNL 3141:192.168.38.18:3141 -o ConnectTimeout=10 -o LogLevel=Error -o StrictHostKeyChecking=no vagrant@192.168.38.18
end script
$
$ # Check the syntax
$ init-checkconf /etc/init/portforward.conf
$ # Start the job
$ sudo service portforward start

Source:
https://www.digitalocean.com/community/tutorials/the-upstart-event-system-what-it-is-and-how-to-use-it

Categories: bash Tags: ,

A Powerline style prompt for your shell

July 23, 2017 Leave a comment

For a hassle free powerline like shell. Works with bash and zsh.

* Shows some important details about the git/svn/hg/fossil branch (see below)
* Changes color if the last command exited with a failure code
* If you’re too deep into a directory tree, shortens the displayed path with an ellipsis
* Shows the current Python virtualenv environment
* It’s easy to customize and extend. See below for details.

https://github.com/banga/powerline-shell

Categories: bash, zsh Tags: