mkdir from grep and pipe - grep

I have this little script that puts out some dir paths.
I want to mkdir all those paths.
for filename in $(git ls-files | grep .java | grep -v 'com/foo' | sed -e 's/\(.java[^/]*\).java/\1/' | uniq)
do
echo "$filename" | sed -e 's/com\/old\/old/com\/new/' | sed 's/\(.*\)\/.*/\1/'
done
So new dir are created... com/old/old = com/new
But I cannot get that mkdir to work... I tried..
path=$("$filename" | sed -e 's/com\/old\/old/com\/new/' | sed 's/\(.*\)\/.*/\1/')
mkdir -p "$path"
That is just messing with the file contents.

You should be able to do all the substitutions in one sed command, and it can also filter out com/foo. Then pipe the output to a while read loop.
git ls-files | grep .java |
sed -e '/com\/foo/d' -e 's/\(.java[^/]*\).java/\1/' -e 's/com\/old\/old/com\/new/' -e 's/\(.*\)\/.*/\1/' |
uniq | while read path; do
mkdir -p "$path"
done
Here's how to do your git mv:
git ls-files | grep .java |
sed -e '/com\/intuit/d' -e 's/\(.java[^/]*\).java/\1/' | uniq |
while read path; do
dir=$(echo "$path" | sed -e 's/com\/old\/old/com\/new/' -e 's/\(.*\)\/.*/\1/')
mkdir -p "$dir"
git mv "$path" "$dir"
done

Related

How to run docker container and login with that user which username we are giving to system through "echo" or std input?

I am trying to create docker container with below dockerfile and what i am trying to do is I am trying to build this Dockerfile and What i am looking is, if I'll run container, then it should ask from me that from which user i need to login, I am getting only input like " Enter the Name: " after that It's not going to login with provided username, which i provided to the input.
Please help!
FROM ubuntu:14.04
RUN apt-get -y update && apt-get -y install sudo
ENV VES=14.04
RUN userdel -r Harsh && userdel -r Piyush && userdel -r Sajid
LABEL Name "Harsh Arora"
RUN useradd -ms /bin/bash Harsh && passwd "Harsharora:Harsharora" | chpasswd && adduser Harsh sudo
RUN useradd -m Piyush && passwd "Piyush:Piyush" | chpasswd
RUN useradd -m Sajid && passwd "Sajid:Sajid" | chpasswd
COPY test.sh /tmp/test.sh
ENTRYPOINT ["/tmp/test.sh"]
So, Please find below "test.sh" script for the references.
#!/bin/bash
#This is test script writing for entrypoint example in docker file.
echo -e " Enter The Name: \c" ; read Name
case $Name in
Love)
echo -e " *********************************************************************************************************************"
echo -e "\t\tTHIS CONTAINER RUNNING BY USER $Name"
echo -e " ********************************************************************************************************************* "
;;
Lucky) echo -e " *********************************************************************************************************************"
echo -e "\t\tTHIS CONTAINER RUNNING BY USER $Name"
echo -e " ********************************************************************************************************************* "
;;
Naveen) echo -e " *********************************************************************************************************************"
echo -e "\t\tTHIS CONTAINER RUNNING BY USER $Name"
echo -e " ********************************************************************************************************************* "
;;
*) echo -e " *********************************************************************************************************************"
echo -e "\t\tTHIS CONTAINER RUNNING BY USER $Name"
echo -e " ********************************************************************************************************************* "
;;
esac
$#
su - $Name <<< echo "$Name"

Flutter -ios - pod install gRPC-C++ sed ssl issue

I have been running through this issue when I do pod install in my flutter project. (I'm going to answer this myself as I reached the solution after couple of hours - I hope it's helpful to anybody reading this in the future
Installing firebase_storage (5.0.1)
Installing flutter_local_notifications (0.0.1)
Installing flutter_plugin_android_lifecycle (0.0.1)
Installing fluttertoast (0.0.2)
Installing gRPC-C++ (1.28.2)
[!] /bin/bash -c
set -e
sed -E -i '' 's;#include <openssl/(.*)>;#if COCOAPODS==1\
#include <openssl_grpc/\1>\
#else\
#include <openssl/\1>\
#endif;g' $(find src/core -type f \( -path '*.h' -or -path '*.cc' \) -print | xargs grep -H -c '#include <openssl_grpc/' | grep 0$ | cut -d':' -f1)
find src/core/ third_party/upb/ -type f \( -name '*.h' -or -name '*.c' -or -name '*.cc' \) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "upb/(.*)";#if COCOAPODS==1\
#include "third_party/upb/upb/\1"\
#else\
#include "upb/\1"\
#endif;g'
find src/core/ third_party/upb/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
find src/core/ src/cpp/ -type f \( -name '*.h' -or -name '*.c' -or -name '*.cc' \) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "(.*).upb.h";#if COCOAPODS==1\
#include "src/core/ext/upb-generated/\1.upb.h"\
#else\
#include "\1.upb.h"\
#endif;g'
find src/core/ src/cpp/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
sed: can't read s;#include <openssl/(.*)>;#if COCOAPODS==1\
#include <openssl_grpc/\1>\
#else\
#include <openssl/\1>\
#endif;g: No such file or directory
In the ios folder run
brew unlink gnu-sed
then
Pod install
You need to remove the gnu-sed Path variable.
/usr/local/Cellar/gnu-sed/4.8/libexec/gsed

Dockerfile String interpolation dosent work

I have been trying to run Dockerfile with the below command.
RUN NODE_VERSION=$( \
curl -sL https://nodejs.org/dist/latest/ | \
tac | \
tac | \
grep -oPa -m 1 '(?<=node-v)(.*?)(?=-linux-x64\.tar\.xz)' | \
head -1 \
) \
&& echo $NODE_VERSION \
&& curl -SLO "https://nodejs.org/dist/latest/node-v$NODE_VERSION-linux-x64.tar.xz" -o "node-v$NODE_VERSION-linux-x64.tar.xz" \
&& curl -SLO "https://nodejs.org/dist/latest/SHASUMS256.txt.asc" \
&& gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
&& grep " node-v$NODE_VERSION-linux-x64.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
&& tar -xJf "node-v$NODE_VERSION-linux-x64.tar.xz" -C /usr/local --strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt
However, for some reason, I see the echo $NODE_VERSION outputs the version details, but, the NODE_VERSION details are not available in the subsequent curl command. What could be going wrong?
It would seem that your output assigned to $NODE_VERSION contains a newline which will cause most of your commands to error out.
You would want to strip the newlines from the output. Something similar to the following:
NODE_VERSION=$( \
curl -sL https://nodejs.org/dist/latest/ | \
grep -oPa -m 1 '(?<=node-v)(.*?)(?=-linux-x64\.tar\.xz)' | \
head -1 | \
tr -d '\r\n' \
)
That should now get your output without any newlines. I removed the tac | tac as that seems redundant.

Docker and running ktutil without it exiting

I am trying to create a keytab file when i build the image.
Here is what I am running on one of our Red Hat boxes:
ktutil
ktutil: add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts
Password for $user_id#DOMAIN.COM:
ktutil: wkt $user_id.keytab
ktutil: quit
and it generates the keytab.
I am trying to do this on Docker and I am running:
RUN ktutil && echo "add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts" && echo "$user_pass" && echo "wkt $user_id.keytab" && echo "quit"
Its doing this:
Step 22/27 : RUN ktutil && echo "add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts" && echo "$user_pass" && echo "wkt $user_id.keytab" && echo "quit"
---> Running in b186efb561fc
ktutil: add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts
$user_pass
wkt $user_id.keytab
quit
So it runs the first command and then exits ktutil? How should i format the RUN command. Is there a trick to getting this to stay in ktutil?
This question is not really Docker specific. It is about how to run ktutil in non-interactive mode and I found existing question which covers that: Script Kerberos Ktutil to make keytabs.
We can apply ideas from that answer to create keytab file in Docker:
FROM centos
# These variables just for demonstration here,
# ideally should be passed as
ARG user_id
ARG user_pass
# Should check here whether the above arguments
# have been actually passed to the build
# Install dependencies
# Add new entry to keytab file and list all entries afterwards
RUN yum install -y krb5-workstation.x86_64 \
&& echo -e "add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts\n$user_pass\nwkt $user_id.keytab" | ktutil \
&& echo -e "read_kt $user_id.keytab\nlist" | ktutil
wkt $user_id.keytab" | ktutil \
&& echo -e ""
When I run the build for the above Dockerfile with this command:
docker build -t ktutil --build-arg user_id=test --build-arg user_pass=test_pass .
I can see the following results:
ktutil: add_entry -password -p test#DOMAIN.COM -k 1 -e aes256-cts
Password for test#DOMAIN.COM:
ktutil: wkt test.keytab
ktutil: ktutil: read_kt test.keytab
ktutil: list
slot KVNO Principal
---- ---- ---------------------------------------------------------------------
1 1 test#DOMAIN.COM
Try:
ktutil: add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts
Password for $user_id#DOMAIN.COM:
ktutil: add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts
Password for $user_id#DOMAIN.COM:
ktutil:
We managed to fix it like so:
RUN printf 'add_entry -password -p $user_id#DOMAIN.COM -k 1 -e aes256-cts\n$user_pass\nwkt $user_id.keytab' | ktutil

How to execute script during image build in Docker

I have the following at the end of my Dockerfile:
ENTRYPOINT bash -C '/usr/local/bin/setup_php_settings';'bash'
If I am not wrong that /usr/local/bin/setup_php_settings will be executed each time the container is started. If so then I have a few installation stuff (like this ZRay for example) inside that script that I would like to move to another script that would be executed just once on image build process let's say.
The content of the setup_php_settings (whithout the ZRay part) is the following:
#!/bin/bash -x
set -e
PHP_ERROR_REPORTING=${PHP_ERROR_REPORTING:-"E_ALL & ~E_DEPRECATED & ~E_NOTICE"}
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/apache2/php.ini
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php5/cli/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/apache2/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php5/cli/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/apache2/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php5/cli/php.ini
mkdir -p /data/tmp/php/uploads
mkdir -p /data/tmp/php/sessions
mkdir -p /data/tmp/php/xdebug
chown -R www-data:www-data /data/tmp/php*
ln -sf /etc/php5/mods-available/zz-php.ini /etc/php5/apache2/conf.d/zz-php.ini
ln -sf /etc/php5/mods-available/zz-php-directories.ini /etc/php5/apache2/conf.d/zz-php-directories.ini
ln -sf /usr/share/php/libzend-framework-php/Zend/ /usr/share/php/Zend
a2enmod rewrite
php5enmod mcrypt
# Apache gets grumpy about PID pre-existing files
: "${APACHE_PID_FILE:=${APACHE_RUN_DIR:=/var/run/apache2}/apache2.pid}"
rm -f "$APACHE_PID_FILE"
source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND "$#"
The question is, how do I execute such new script during image build? Using CMD? Any other workaround?
The correct directive for this is RUN. This would run the script on image build. in your case -
RUN /usr/local/bin/setup_php_settings
CMD bash

Resources