PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 22, 2022

[FIXED] How to combine exec commands in shell script

 July 22, 2022     bash, exec, postgresql     No comments   

Issue

I am running postgreSQL on docker. In order to access the container I run

winpty docker exec -it postgres_db bash

Now that I am inside the docker container, I run this to access as user 'postgres' in postgres server

psql -U postgres

Then what I want to do is create a database

create database test;

I can I do this sequentially in a script? I want something like this:

#!/bin/bash
winpty docker run -p 8005:5432 --name postgres_db -e POSTGRES_PASSWORD=password -d postgres
sleep 5
winpty docker exec -it postgres_db bash -c "psql -U postgres" [-c "create database test;"]

create database cannot be executed if im not inside "psql -U postgres". Obviously the last line -c "create database test;" is wrong, just to make you understand what I want to do.


Solution

winpty docker exec -it postgres_db bash -c "psql -U postgres -c 'CREATE DATABASE test;'"

Both bash -c and psql -c take one shell word as input. A single or double quoted string is one word. You need to nest the quotes, just like the -c commands are nested.

Alternative:

winpty docker exec -it postgres_db bash -c "psql -U postgres -c \"CREATE DATABASE test;\""


Answered By - dan
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing