Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script removing first letter

Tags:

bash

I have a bash script that I'm using to migrate some IMAP mail accounts that reads from a config file.

It works for single line entries, but when I have multiple accounts only the first entry works and the rest have the first character of the username removed for some reason. Also the command I'm using to pause and wait for a keystroke isn't pausing. (I can debug that part but I'll certainly upvote you if I don't have to!)

I'm 99% sure my problem is the only line I don't fully understand:

CREDS=(${VAR// / })

Also notable is passwords are chock full of special characters but no spaces. Here's the code:

#! /bin/bash
# Script to help migrate email account contents
# must have accounts all ready set up and using IMAP
# uses hardcoded dovecot master user on host2 too bad host1 is stupid courier

# uses file mailusers.cfg for users and passwords
# one entry per line - format is:
# [email protected] passwordstring

while read p; do
  VAR=$p
  CREDS=(${VAR// / })
  VARUSER=${CREDS[0]}
  VARPASS=${CREDS[1]}
  OUTPUT="Username: $VARUSER Password: $VARPASS"
  echo $OUTPUT
  imapsync --host1 123.456.789.123 --user1 $VARUSER --password1 $VARPASS --authmech1 PLAIN --host2 999.888.777.123 --user2 $VARUSER*[email protected] --password2 notreallypasswordXD --authmech2 PLAIN --exclude 'Trash|Spam'
  echo "Done!"
  echo
  read -n 1 -s
done < mailusers.cfg

Note: This script shouldn't be run on a system where other users are logged in because they can see the username/passwords if they happen to run ps aux I believe. See imapsync docs for more details on that.

like image 648
Syntax Error Avatar asked Feb 11 '26 15:02

Syntax Error


1 Answers

The reason you're missing the first character of the usernames is precisely the same as the reason your pause doesn't work: your pause (read -n 1 -p) reads one character from stdin. However, stdin has been redirected to the datafile, so it reads the first character of the username.

You really need to quote variables in expansions. Get in the habit of doing so. It's much safer.

I don't know what the replacement line does either: as you present it, it looks like a no-op but I suppose it's possible that one of the spaces is a tab. Anyway, it is unnecessary. You can replace all of that fooling around with shell variables like this:

while read -r VARUSER VARPASS ignore_rest_of_line; do
  echo "Username: $VARUSER Password: $VARPASS"
  imapsync --host1 123.456.789.123 --user1 "$VARUSER" \
           --password1 "$VARPASS" --authmech1 PLAIN \
           --host2 999.888.777.123 \
           --user2 "$VARUSER*[email protected]" \
           --password2 notreallypasswordXD \
           --authmech2 PLAIN --exclude 'Trash|Spam'
  echo "Done!"
  echo
  read -n 1 -s < /dev/tty
done < mailusers.cfg
like image 151
rici Avatar answered Feb 18 '26 02:02

rici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!