Sun Grid Engine: name output file using value stored in variable - bash

Thanks in advance for the help.
I am trying to pass a job using
qsub -q myQ myJob.sh
in myJob.sh I have
# Name of the output log file:
temp=$( date +"%s")
out="myPath"
out=$out$temp
#$ -v out
#$ -o $out
unset temp
unset out
What I want is for my output file to have standard name with the unix timestamp appended to the end such as myOutputFile123456789
When I run this, my output file is named literally "$out" rather than myOutputFile123456789. Is it possible to do what I want and if so how might I do it?

You can't set -o or -e programtically inside the script. What you can do is point them at /dev/null then redirect inside the script. Assuming you want the timestamp to be the time the job ran and the jobscript is a bourne shell script (including bash,ksh,zsh scripts) then the following should work
#$ -o /dev/null
exec >myPath$(date +"%s")
You'll be throwing away any output from the prolog/epilog though.

Related

Adding printers by shell script; works in terminal but not as .command

I am trying to provide a clickable .command to set up printers in Macs for my workplace. I thought since it is something I do very frequently, I can write a shellscript for each printer and save it on a shared server. Then, when I need to add a printer for someone, I can just find the shell script on the server and execute it. My current command works in terminal, but once executed as a .command, it comes up with the errors.
This is my script:
#!/bin/sh
lpadmin -p ‘PRINTERNAME’ -D PRINTER\ NAME -L ‘OFFICE’ -v lpd://xx.xx.xx.xx -P /Library/Printers/PPDs/Contents/Resources/Xerox\ WorkCentre\ 7855.gz -o printer-is-shared=false -E​
I get this error after running the script:
lpadmin: Unknown option “?”.
I find this strange, because there is no "?" in the script.
I have a idea, why not try it like this ? there are huge differences between sh shells, so let me know if it rocks, I have more ideas.
#!/bin/sh
PPD="PRINTERNAME"
INFO="PRINTER\ NAME"
LOC="OFFICE"
URI="lpd://xx.xx.xx.xx"
OP ="printer-is-shared=false"
# This parameter P is new to me. Is it the paper-name ?
P="/Library/Printers/PPDs/Contents/Resources/Xerox\ WorkCentre\ 7855.gz"
lpadmin -p "$PPD" -D "$INFO" -L "$LOC" -v "$URI" -P "$P" -o "$OP" -E;

Bash pass date variable as string

I have a script which creates another script when run like this:
cat > "$installpath""tweets.sh" << ENDOFFILE
#!/bin/bash
source "$installpath"config.sh
cd \$webdir
/usr/local/bin/twint -s "\$search" --limit \$scrapelimit -o \$csvname --csv --database \$dbfile -ho
FILE=\$csvname
NAME=\${FILE%.*}
EXT=\${FILE#*.}
DATE=`\date +%d-%m-%Y-%H-%M`
NEWFILE=\${NAME}_\${DATE}.\${EXT}
echo \$NEWFILE
mv \$csvname \$NEWFILE
export NEWFILE
export DATE
ENDOFFILE
However, the script interprets the
DATE=`\date +%d-%m-%Y-%H-%M`
and changes it to
DATE=28-09-2019-15-49
I have tried escaping the variables in every possible way but nothing seems to work. Any ideas?
I suggest to use:
DATE=\$(date +%d-%m-%Y-%H-%M)

Add string to default SAS log file name from bash script

I'm trying to build a simple bash script to automate running some monthly SAS programs at work. The problem I'm running into is that we like to keep logs based on the day a program was run, in case the underlying data changes, but I can't find a way to append the date to the log file.
My base code is as follows:
#!/bin/bash
month=`date +%Y%m -d "1 month ago"` #Previous month for log folder
sysdate=`date "+%Y_%m_%d"` #today's date
sasbatdir=/c01/sasdata/public
sasdir=/n04/directory-where-programs-are
saslog=/n04/directory-where-programs-are/Log/$month
cd $sasdir
$sasbatdir/batchsas.sh -s PROGRAM_01.sas -o $saslog -k traditional
$sasbatdir/batchsas.sh -s PROGRAM_02.sas -o $saslog -k traditional
$sasbatdir/batchsas.sh -s PROGRAM_03.sas -o $saslog -k traditional
... etc
exit 0
So the above works, but it obviously only outputs log files with the name PROGRAM_01.log, PROGRAM_02.log, etc. format, which get overwritten the next time the script is run in that month.
Things I have tried:
$sasbatdir/batchsas.sh -s PROGRAM_01.sas -o $saslog/PROGRAM_01_"$sysdate".log -k traditional
and
$sasbatdir/batchsas.sh -s PROGRAM_01.sas -log $saslog/PROGRAM_01_"$sysdate".log -k traditional
Does not work. nohup returns an "Output directory not found" error, and appears to be treating the log name as a directory instead of a file.
$sasbatdir/batchsas.sh -s PROGRAM_01.sas -o -t 1 > $saslog/PROGRAM_01_"$sysdate".log -k traditional 2>&1
Mostly works, but returns two log files: one with the correct name, but only containing the nohup output, and the other with the SAS log, but with both the date (in the wrong format) and the job ID appended. Removing the 2>$1 prevents either from being written. I'd honestly take the second one, if I could figure out how to produce it without the first, though I would prefer to stick to the Program_Name_YYYY_MM_DD.log format.
In case it's relevant, the command I'm using to test the programs is nohup /n04/directory-where-program-is-stored/test_script.sh
I would add the following command:
exec > "$saslog/PROGRAM_01_${sysdate}.log" 2>&1
It would be preferible to add this command inside the "batchas.sh" script, but in case it is not possible, you can add this line before every "batchas.sh" script call with the new log file.
This command redirects stdout and stderr to the indicated file, regardless the script was launched from command line, crontab or with nohup.

shell script variable being truncated - why

I have been trying to customise this very useful (in principle) backup to s3 script.
I really am not a shell scripter to any real level and I can't work out why this line
is truncating the variable.
so e.g.
DB=abcdefg
abcdefg_USER=testuser
USER=$(eval echo \$${DB}_USER)
The eval statement is returning bcdefg_USER so is truncating the variable and echoing out bcdefg_USER not abcdefg_USER and so isn't evaluating the variable abcdefg_USER
Running on an amazon linux ec2 instance.
Anyone explain to me what I am missing, I've tried playing around with the escaping and braces etc and echoing out each stage in the process but can't get a handle on what is going on.
Thanks
full script below:
## Specify data base schemas to backup and credentials
DATABASES="wp myotherdb"
## Syntax databasename as per above _USER and _PW
wp_USER=username
wp_PW=password
myotherdb_USER=username
myotherdb_PW=password
## Specify directories to backup (it's clever to use relaive paths)
DIRECTORIES="/var/www root etc/cron.daily etc/cron.monthly etc/apache2 etc/mysql etc/php5"
## Initialize some variables
DATE=$(date +%d)
BACKUP_DIRECTORY=/tmp/backups
S3_CMD="s3cmd"
## Specify where the backups should be placed
S3_BUCKET_URL=s3://mybackupbucket/$DATE/
## The script
cd /
mkdir -p $BACKUP_DIRECTORY
rm -rf $BACKUP_DIRECTORY/*
## Backup MySQL:s
for DB in $DATABASES
do
BACKUP_FILE=$BACKUP_DIRECTORY/${DB}.sql
USER=$(eval echo \$${DB}_USER)
PASSWORD=$(eval echo \$${DB}_PW)
/usr/bin/mysqldump -v -u $USER --password=$PASSWORD -h localhost -r $BACKUP_FILE $DB 2>&1
gzip $BACKUP_FILE 2>&1
$S3_CMD put ${BACKUP_FILE}.gz $S3_BUCKET_URL 2>&1
done
## Backup of config directories
for DIR in $DIRECTORIES
do
BACKUP_FILE=$BACKUP_DIRECTORY/$(echo $DIR | sed 's/\//-/g').tgz
tar zcvf ${BACKUP_FILE} $DIR 2>&1
$S3_CMD put ${BACKUP_FILE} $S3_BUCKET_URL 2>&1
done
Assuming that you are using bash, this is how to avoid eval:
$ DB=abcdefg
$ abcdefg_USER=testuser
$ tmpvar=${DB}_USER
$ USER=${!tmpvar}
$ echo $USER
testuser
If you have bash version 4, consider using associative arrays:
$ declare -A users
$ users[abcdefg]=testuser
$ echo "${users[$DB]}"
testuser
You're running into some weird bug involving command substitution and echo.
When using eval to access a computed variable name, it is not necessary to complicate things by involving echo wrapped in a process substitution. Try this pattern, which should work pretty much in any POSIX-like shell.
eval FINAL_VALUE=\$${COMPUTED_VAR_PREFIX}_FIXED_SUFFIX
That is to say, just generate the source code of the desired variable assignment, and eval that code.

Inserting variables into URLs with bash scripting

Say I want to write a bash script that takes user input, inserts it into a URL and then downloads it. Something like this:
#!/bin/bash
cd /path/to/folder
echo Which version?
read version
curl -O http://assets.company.tld/$version/foo.bar
Would this work? If not, how can I do what I'm trying to do?
#!/bin/bash
version=$1
cd /path/to/folder
echo $version
curl -o $version-foo.bar http://assets.company.tld/$version/foo.bar
where $1 is the first positional argument
So, suppose you save the script with name assets.sh. Then you can using the same like following:
./assests.sh ver1
where ver1 is the version
[EDIT] If you want an interactive session:
#!/bin/bash
version=$1
cd /path/to/folder
echo -n "Which version you want? "
read version
curl -o $version-foo.bar http://assets.company.tld/$version/foo.bar

Resources