Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Monday, August 2, 2021

Enter into the command prompt or shell of a running Docker container from the host command line

If you need to enter into the command prompt of a running Docker container:

sudo docker ps

And then enter the container by name or ID

sudo docker exec -i -t nginx-test /bin/bash

or

sudo docker exec -i -t e7a5s8dd2s3s4-test /bin/sh


Sunday, May 21, 2017

Linux login as www-data user

If you ever need to login as apache (www-data) user to test permissions on a file share, you can use the following command to impersonate yourself as the Apache user:

root@rp:~# su - www-data -s /bin/bash
www-data@rp:~$ cd /var/lib/phpfina
www-data@rp:/var/lib/phpfina$ vi test

Thursday, January 12, 2017

Alternative way to be testing command line if port is open or closed

Trying to find out on a Linux server or under Busybox (Synology) if a TCP port is open or closed? Use this handy command:

exec 6<>/dev/tcp/127.0.0.1/443 || echo "Port is not open"
exec 6>&- # close output connection
exec 6<&- # close input connection

6 is used as the file descriptor. 0,1,2 are stdin, stdout, and stderr. 5 is sometimes used by Bash for child processes, so 3,4,6,7,8, and 9 should be safe.

Alternatively, if the port you're probing is serving the HTTP(S) protocol:

exec 6<>/dev/tcp/127.0.0.1/443
echo -e "GET / HTTP/1.0\n" >&6
cat <&6

Alternative ways are listed here.

Thursday, March 5, 2015

Add leading zero to integer in bash

I was composing a script that would store a file per day in the month, but wanted to have a leading zero not to mix up when dir listing. In bash, you have a couple of options.

1. Using range
#!/bin/bash
for i in {01..31}
do
   echo "Day: $i"
done

Works, but has downside that you cannot feed it with a variable.

2. Three-expression example
#!/bin/bash
for (( i=1; i<=31; i++ ))
do
   i=$(("0"$i))
   echo "Day: $i"
done

Will throw an error being: value too great for base (error token is “08”) Reason being that Bash will treat the variable as an octal, even with leading zero. Prepending the string "10#" to the front of your variable will have it treated as decimal by Bash.

3. Using printf
#!/bin/bash
for (( i=1; i<=31; i++ ))
do
   i=$(printf "%02d" $i)
   echo "Day: $i"
done

Works & easy solution, my favorite.

4. Using seq
#!/bin/bash
for i in `seq -f ‘%02g’ 1 31`
do
   echo "Day: $i"
done

Works also

Monday, March 2, 2015

Synology default shell does not support regex

I was trying to write a script today that would parse a file and check if a line matches a regex. It would fail for unknown reasons, until I found out that the default shell within a Synology (ash), does not support this. Installing bash with ipkg solves this.

Script (not working in ash):
#!/bin/sh

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

  if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi

done < "$FILENAME"

In my terminal:
$ ./status.sh
sh: 2.3.6: unknown operand

Script (working under bash):
#!/opt/bin/bash

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

 if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi
done < "$FILENAME"

In my terminal:
$ ./status.sh
./status.sh
No match: 1
No match: 2
No match: 3
Match CLIENT_LIST: 4
No match: 5
No match: 6
No match: 7

Tip from: http://forum.synology.com/enu/viewtopic.php?f=27&t=77899