# Shell

.<https://cloud.google.com/shell/docs/configuring-cloud-shell>

Source: <https://github.com/confluentinc/examples/blob/6.1.0-post/clickstream/ksql/ksql-clickstream-demo/demo/ksql-tables-to-grafana.sh#L18>

```bash
declare -a tables=('click_user_sessions' 'user_ip_activity');
for i in "${tables[@]}"
do
    table_name=$i
    TABLE_NAME=`echo $table_name | tr '[a-z]' '[A-Z]'`

    ## Cleanup existing data

    echo -e "\t-> Remove any existing Elastic search config"  
    curl -s -X "DELETE" "http://$ELASTIC_HOST:9200/""$table_name"  >>/tmp/log.txt 2>&1

done
```

/run

![](/files/-MVSeT6S1SInscKo9jjK)

Library

.<https://github.com/GoogleCloudDataproc/initialization-actions/blob/master/cloud-sql-proxy/cloud-sql-proxy.sh>

```bash
function get_hiveserver_uri() {
  local base_connect_string="jdbc:hive2://localhost:10000"
  if [[ "${KERBEROS_ENABLED}" == 'true' ]]; then
    local hive_principal
    hive_principal=$(get_hive_principal)
    echo "${base_connect_string}/;principal=${hive_principal}"
  else
    echo "${base_connect_string}"
  fi
}
```

cat command

.<https://github.com/GoogleCloudDataproc/initialization-actions/blob/master/cloud-sql-proxy/cloud-sql-proxy.sh#L276-L292>

```bash
cat <<EOF >hive-template.xml
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://localhost:${METASTORE_PROXY_PORT}/${METASTORE_DB}</value>
    <description>the URL of the MySQL database</description>
  </property>
 ...
</configuration>
EOF
```

Permissions

```bash
sudo chown -R $USER:$(id -gn $USER) /home/janardhan/.config
```

.<https://github.com/git/git/blob/e6362826a0409539642a5738db61827e5978e2e4/t/t1301-shared-repo.sh#L74-L84>

```bash
for u in    0660:rw-rw---- \
        0640:rw-r----- \
        0600:rw------- \
        0666:rw-rw-rw- \
        0664:rw-rw-r--
do
    x=$(expr "$u" : ".*:\([rw-]*\)") &&
    y=$(echo "$x" | sed -e "s/w/-/g") &&
    u=$(expr "$u" : "\([0-7]*\)") &&
    git config core.sharedrepository "$u" &&
    umask 0277 &&
```

### history

**`Ctrl`** + **`R`** - Recursive command search

```bash
$ history

clear
echo "hello"
history

# Replay above three commands by !3
# Which tells 3rd line to run
!3

$ history | grep hello
3 echo "hello"
5 history | grep hello


# For timestamp
HISTTIMEFORMAT="%d/%m/%y %T "  # for e.g. “29/02/99 23:59:59”
HISTTIMEFORMAT="%F %T "        # for e.g. “1999-02-29 23:59:59”
```

### Environment variables

#### Windows (PowerShell)

```bash
($env:NODE_ENV = "development") -and (nx build myapp)
```

#### Windows (cmd.exe)

```bash
set "NODE_ENV=development" && nx build myapp
```

#### Unix systems

```bash
NODE_ENV=development nx build myapp
```

### Java Dev Environment

```bash
sudo apt-get update
sudo apt-get install git -y
sudo apt-get install -yq maven

# configure iptable requests from port 80 to 8080
# Java web application listens on 8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
```

### PGP

```bash
PGP_PUB_KEY="generated-key.pgp"
sudo apt-get install rng-tools
sudo rngd -r /dev/urandom
gpg --quick-generate-key --yes j143@protonmail.com
gpg --armor --export "${ATTESTOR_EMAIL}" > ${PGP_PUB_KEY}
```

```bash
GENERATED_PAYLOAD="generated_payload.json"
GENERATED_SIGNATURE="generated_signature.pgp"

PGP_FINGERPRINT="$(gpg --list-keys ${ATTESTOR_EMAIL} | head -2 | tail -1 | awk '{print $1}')"
# ignore gcloud command, focus on generated payload
gcloud beta container binauthz create-signature-payload \
    --artifact-url="${IMAGE_PATH}@${IMAGE_DIGEST}" > ${GENERATED_PAYLOAD}
gpg --local-user "${ATTESTOR_EMAIL}" \
    --armor \
    --output ${GENERATED_SIGNATURE} \
    --sign ${GENERATED_PAYLOAD}
```

### /etc/profile

```
echo "gcloud container clusters get-credentials gke-security-demo-ss \
--zone us-central1-a --project qwiklabs-gcp-03-173a36e0912c" >> /etc/profile
echo "kubectl get clusterrolebinding gke-tutorial-admin-binding &> /dev/null ||
kubectl create clusterrolebinding gke-tutorial-admin-binding \
--clusterrole cluster-admin --user $(gcloud config get-value account)
" >> /etc/profile
```

### Install Ansible terraform

```bash
# Install Ansible package from apt repository.
function getAnsible() {
  local A_EXE="/usr/bin/ansible"
  if [ -e ${A_EXE} ]; then
    echo "${A_EXE} already exists. Exiting."
    return 0
  fi
  echo -n "Installing Ansible ..."
  sudo apt-get -y update &> setup.log
  sudo apt-get -y install software-properties-common &>> setup.log
  sudo apt-add-repository ppa:ansible/ansible -y &>> setup.log
  sudo apt-get -y update &>> setup.log
  sudo apt-get -y install ansible &>> setup.log
  if [ -e ${A_EXE} ]; then
    echo " Done"
  else
    echo " Could not retrieve ${A_EXE}."
  fi
}
# Install Python libraries.
function getPyLibs() {
  echo -n "Installing Python libraries ..."
  sudo pip install pandevice xmltodict &>> setup.log
  echo " Done"
}
# Main program
getAnsible
getPyLibs
```

### sed

```bash
sed -i 's_~/_~_' ./scripts/install/setup_properties.sh
```

![](/files/-MYqzqAgcKGYNZ703Z1i)

![](/files/-MYqzudpp_rcHvfKE3Dn)

```bash
sed -i 's|= container|= k8s_container|g' filename.txt
```

### .sh examples

```bash
#!/bin/bash
sudo apt-get update
sudo apt install git kubectl -y
mkdir /work
chmod o+rwx /work 
cd /work
gsutil cp gs://spls/gsp314/files.tgz .
tar -xzf files.tgz
chmod o+rw -R *
cd /
gsutil cp gs://spls/gsp314/script.sh .
chmod +x script.sh
useradd -m user1
echo "Starting VM provisioning Script"  
su -c "/script.sh student-04-f568db16783a@qwiklabs.net" - user1
if [$? -eq 1]
then
  echo "Script failed. Aborting DM."
  exit 1
else
  echo "Signal the waiter we have completed successfully"
  gcloud beta runtime-config configs create projects/qwiklabs-gcp-04-b0801511bcde/configs/qldm-27678628-3f7856d82ec5c7b5-installer-config --description "Jumphost VM"
  gcloud beta runtime-config configs variables set \
    success/jumphost-waiter \
    success --config-name projects/qwiklabs-gcp-04-b0801511bcde/configs/qldm-27678628-3f7856d82ec5c7b5-installer-config
fi
```

## Powershell

.<https://cloud.google.com/iam/docs/creating-managing-service-account-keys#uploading>

```bash
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
  -Method POST `
  -Headers $headers `
  -ContentType: "application/json; charset=utf-8" `
  -InFile request.json `
  -Uri "https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com/keys:upload" | Select-Object -Expand Content
```

Tips

How to remove a file named `-r`

`-r` is normally a flag, but here it is accidentally created

```bash
[root@machine ~]# ls
-r                        logs-delete.sh           nohup.out
archive                           trash

```

```bash
[root@machine ~]# rm -- -r
rm: remove regular empty file '-r'? y
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code.janardhanpulivarthi.com/ubuntu/shell.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
