Write $(date) Into File Using Cron and Bash Shell Script

Ubuntu 18.04 running as guest on MacOS 10.14.3. Hypervisor: VMWare Fusion 10

Objective: Every minute, append the date/time into a file

Status: Script runs every minute, inserting a blank new line into the text file. The date is not included. That is the problem

When the script is run from the command line, using the same account as the account on the crontab, the date is added to the file, no problem.

Apparent Problem: Echoing date to file works from command line, but same command from cron does not produce date

This is the script that runs every minute in cron.

#!/bin/bash #append_date_to_file.sh target_file='/home/dan/timestamps' echo $  (date)  >>  $  target_file 

This is the cron entry

crontab -e  PATH=/bin/bash: *       *       *       *       *       /home/dan/append_date_to_file.sh 

RESULTS When running three times from command line, then viewing the target file:

$   cat /home/dan/timestamps Sat Aug 17 18:31:12 CDT 2019 Sat Aug 17 18:31:33 CDT 2019 Sat Aug 17 18:31:34 CDT 2019 

Then waiting three minutes, to let it run three times in cron, then viewing the target file: (same as above, except three new blank lines after the dates.)

Problems Considered .. file permissions. To address this, here is the file as it is listed with permissions and owner:group:

-rwxrwxrwx 1 dan dan  157 Aug 17 18:43 timestamps* 

.. Script not running in cron. Resolved: script is proven to run by the fact that modified time on the file changes at the start of every minute and the file gets a new blank line at the same time.

I cannot think of an explanation for the date not being added to the file when the script is run by cron instead of the command line.

Any ideas would be appreciated.

Thank you.

Nautilus error message when launched from Bash (Gnome) terminal

When the Nautilus file explorer is launched from the command line, the following error sometimes occurs:

Nautilus-Share-Message: [TIME]: Called "net usershare info" but it failed: Failed to execute child process “net” (No such file or directory) 

The bash terminal shows a strange extra line and you must either hit SPACE or Ctrl+Z to exit when you are returned to the normal Gnome prompt.

This happens with all of the following:

* `nautilus --browser` or `nautilus --browser PATH` * `gnome-open .` or `gnome-open PATH` * `gio open .` or `gio open PATH` 

The error does not appear every time:

1) Typically, the error does not occur at all

2) Sometimes Nautilus is launched, the cursor returns to the prompt briefly, and then the error appears a fraction of a second later. In this case, when Terminal is closed, the Nautilus file explorer stays alive.

3) Sometimes the error occurs before the Nautilus file explorer is displayed. In this case, if Terminal is closed a box pops up warning that closing Terminal will also terminate another blocked process. When Terminal is closed, the Nautilus file explorer is also terminated.

Looks like a timing issue. Any ideas what is causing this?

Can’t get bash working with i3wm

No matter what i try i can’t get ash terminal to work on i3wm apart from xterm, all i wanted was a bash window.

Have the same problem as this thread only xterm works so all the solutions are useless

(Noob question) – fresh install, i3 starting but not opening terminal windows or allowing a quit back to virtual console. from archlinux

So far only xterm lxterm and gnome-terminal worked

Ruta absoluta en una variable bash

Tengo un ejercicio en el que debo solicitar rutas absolutas de tres directorios en una terminal, una IP de un host y un directorio de ese host, donde debo copiar los tres primeros directorios, mi intento es el siguiente:

#!/bin/bash dir1="" dir2="" dir3="" dirDest="" ip="" echo "COPIA DE DIRECTORIOS POR SSH" read -p"Primer directorio a copiar (Ruta absoluta): " dir1 read -p "Segundo directorio a copiar (Ruta absoluta): " dir2 read -p "Tercer directorio a copiar (Ruta absoluta): " dir3 read -p "Directorio de destino (Ruta absoluta): " dirDest read -p "Indique IP del host: " ip  scp -pr "$  dir1 $  ip:$  dirDest" scp -pr "$  dir2 $  ip:$  dirDest" scp -pr "$  dir3 $  ip:$  dirDest" 

El problema es que me sale el siguiente error: ´ : not valid identifiered: dirX` siendo la X las variaciones del nombre de las variables y la variable me la muestra vacía. La pregunta es: ¿Como hago que me tome las rutas absolutas y la guarde en las variables?

How to fix HOME and END buttons in Bash with ANSI prompt?

In bash, whenever I have a prompt with ANSI color codes, it messes up the HOME and END functionality. Is there an obvious fix for this?

To re-create:

PS1="\e[0;33m\u@\e[m $   " echo "This is a very long sentence to demonstrate what im talking about" (press UP) (press HOME) 

See where the cursor ends up?

enter image description here

  • Ubuntu 16.04
  • This is a remote login. Is this an SSH problem? Doesn’t seem to be. I’ve tried numerous SSH terminals and VNC from both Mac and PC, all giving same results….

Bash – Copy all .jar files to current directory from subfolders

I have folder named datahub and it has 15 folders and more than 20 subfolder which holds .jar files. I wanted to find all .jar files from subfolders and copy back to my current directory.

I am referring Bash – create zip by finding files but this is copying subfolders as well, but i wanted to copy only .jar’s to my current directory or to create one directory and copy to it.

Thanks in Advance for your help Venkat

How to use case statements in BASH?

I have code which extracts the time stamping’s by matching a key word/sentence of a .log file which is existing in several folders of the form log_Job_*. It calculates time duration’s of different processes and write the output to a .CSV file.

What columns I want to have in my .csv:

  1. FileName
  2. ProcessType
    • Import
    • Validate
  3. . .
  4. MainPartionDuration

Now the problem is, I have 2 types of process under 2. Process Type. So, thought of using case statements here. And did as following:

#!/bin/bash  Header="Filename1,ProcessType1,TotalDuration1,Initialization1,MPEProcessDuration1,TotalPartitionDuration1,WaitPartitionDuration1,MainPartionDuration1" echo $  Header >>OutputFile_Validate.csv  cd /path/to/manoj/version_2019_logs/ for file in log_Job_*/manoj.log; do     ProcessType1="$  (grep -F 'Running process mpeXbrlImport.xml' "$  file" | awk '{print $  5}' | cut -c 4-)" #echo "$  ProcessType" ProcessType2="$  (grep -F 'Running process mpeXbrlValidate.xml' "$  file" | awk '{print $  5}' | cut -c 4-)" #echo "$  ProcessType" ProcessType="$  ProcessType1","$  ProcessType2" case $  ProcessType in  $  ProcessType1)  set of commands to get other variaqbles  Var="$  Filename","$  ProcessType","$  TotalDuration","$  Initialization","$  MPEProcessDuration","$  TotalPartitionDuration","$  WaitPartitionDuration","$  MainPartionDuration" echo $  Var >>OutputFile_Import.csv ;; *)  repeat the set of commands and this time save with different variable names Var1="$  Filename1","$  ProcessType1","$  TotalDuration1","$  Initialization1","$  MPEProcessDuration1","$  TotalPartitionDuration1","$  WaitPartitionDuration1","$  MainPartionDuration1" echo $  Var1 >>OutputFile_Validate.csv ;; esac     done 

With this my plan is to create two seperate .CSV files based on the Processtype and then concatenate both the files.

Problem: The script is getting executed successfully, but at the end it is generating only one file i.e. OutputFile_Validate.csv

I have verified the script carefully, am not using any variables repeatedly. Can someone tell me, what could be the reason for this!!

How to authenticate with a user’s Gitlab username and password through a bash executable

So this is in addendum to my question posed here. Thank you to @dessert who helped a lot with that question.

So I am trying to authenticate a Gitlab user by using their username and password, and not by creating an impersonation token. At the moment, the code that @dessert has created that uses an impersonation token to authenticate a user is as such:

$   curl -d '{"title":"test key","key":"'"$  (cat ~/.ssh/id_rsa.pub)"'"}' -H 'Content-Type: application/json' https://gitlab.com/api/v4/user/keys?private_token=<my_access_token> 

Mainly focusing on the `private_token=, except with the creation of an impersonation token found here

$   curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --data "name=mytoken" --data "expires_at=2017-04-04" --data "scopes[]=api" https://gitlab.example.com/api/v4/users/42/impersonation_tokens 

I want to be able to use the username and password of the Gitlab user to authenticate them. I’ve seen this done with Github, and have done it myself:

$   curl -u "USERNAME:PASSWORD" --data "{\"title\": \"TITLE\", \"key\": \"$  (cat ~/.ssh/id_rsa.pub)\"}" https://api.github.com/user/keys 

Where USERNAME and PASSWORD are authenticating by user-inputted username and password.

I don’t need help with reading input of the user; that’s not the issue. My issue (to sum up) is to be able to authenticate a Gitlab user through their username and password.

Again, thanks to @dessert for the help, and any other assistance is appreciated!!

A quick edit: I found this forum post for authentication with login and password, and most are saying to use a Personal Access Token. Does this mean I have to get the user to give me their PAT for authentication? Or do I provide my PAT and somehow it does something? (I was a little unclear on this)

BASH string dentro de string

Estoy trabajando es rapberry pi 3B y tengo este bash con el que intento buscar el puerto de comunicaciones correspondiente a un determinado dispositivo.

    #!/bin/bash    for sysdevpath in $  (find /sys/bus/usb/devices/usb*/ -name dev); do  (    syspath="$  {sysdevpath%/dev}"    devname="$  (udevadm info -q name -p $  syspath)"    eval "$  (udevadm info -q property --export -p $  syspath)"   # if [[ "$  {ID_SERIAL}" == *"STM32_Virtual_ComPort"* ]] ;then   #  if [[ "$  {ID_SERIAL}" =~ "STM32_Virtual_ComPort" ]] ;then   #  if [[ "$  {ID_SERIAL}" == *"STM32_Virtual_ComPort"* ]] ;then   #  if [[ "$  (echo "$  ID_SERIAL" | tr '[:upper:]' '[:lower:]')" == *"STM32_Virtual_ComPort"* ]] ;then     if [[ "$  {ID_SERIAL,,}" == *"STM32_Virtual_ComPort"* ]] ;then    #if [ $  ID_SERIAL = *'STM32_Virtual_ComPort'* ]; then        echo "/dev/$  devname - $  ID_SERIAL"    fi ) done 

Esta es la lista puerto->dispositivo sin filtrar:

/dev/bus/usb/001/001 – Linux_4.19.58-v7+_dwc_otg_hcd_DWC_OTG_Controller_3f980000.usb

/dev/hidraw1 –

/dev/input/event1 – CHICONY_Asus_Keyboard

/dev/bus/usb/001/005 – CHICONY_Asus_Keyboard

/dev/hidraw2 –

/dev/input/event2 – CHICONY_Asus_Keyboard

/dev/usb/hiddev0 –

/dev/bus/usb/001/003 – 0424_ec00

/dev/bus/usb/001/002 – 0424_9514

/dev/bus/usb/001/011 – 0424_2517

/dev/bus/usb/001/020 – STMicroelectronics_STM32_Virtual_ComPort_00000000001A

/dev/ttyACM0 – STMicroelectronics_STM32_Virtual_ComPort_00000000001A

/dev/hidraw0 –

/dev/input/mouse0 – Logitech_USB_Optical_Mouse

/dev/input/event0 – Logitech_USB_Optical_Mouse

/dev/bus/usb/001/004 – Logitech_USB_Optical_Mouse

El problema esta en el if (como se puede ver por las lineas comentadas, que son las diferentes pruebas que he hecho), que no se realiza correctamente. Si se ejecutase correctamente deveria darme la siguiente salida:

/dev/bus/usb/001/020 – STMicroelectronics_STM32_Virtual_ComPort_00000000001A

/dev/ttyACM0 – STMicroelectronics_STM32_Virtual_ComPort_00000000001A