what is use of `test -x` in Perl script execution [duplicate] - linux

This question already has answers here:
what does command test -x do in ubuntu?
(2 answers)
Closed 3 years ago.
I am new to Perl !!!. I have one old crontab entry in sun OS machine to run a Perl script which goes like 10 * * * * test -x $path/abc.pl && $path/abc.pl >/dev/nul.
I want to know what is the use of term test -x in this execution.

test check file types (and permissions).
test -x FILE checks that FILE exists and execute (or search) permission is granted.
So, only if $path/abc.pl is an existing file and the current user has execute permission, it will be executed.
Refernce:
https://linux.die.net/man/1/test
BTW, this has nothing to do with Perl, although Perl has similar function:
https://perldoc.perl.org/functions/-X.html

Related

Bash Script: `cd` with command substitution [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Change the current directory from a Bash script
(17 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
Summary:
I am trying to have a script that allows easy one-touch access to files that are generated by the crontab.
The hourly generated files go by the path as: /home/mouse/20210126/0900.
Of course as the date and time changes, the path will change like so:
/home/mouse/20210126/1000
/home/mouse/20210126/1100
/home/mouse/20210127/1000
/home/mouse/20210128/1300
I tried using an alias, but once .bashrc is loaded, it only takes the present date/time, which means that I cannot "refresh" the date and time.
So by using a script, I could update the date and time like so:
currentdate=$(date +%Y%m%d)
currenttime=$(date -d '1 hour ago' "+%H00")
collect=cd /home/mouse/$currentdate/$currenttime
echo $currentdate
echo $currenttime
$collect
However, I realized that the cd script is not working because, from my understanding, it works on a subshell and once the script has finished executing, it does not affect the main shell.
I tried source / . but I get /home/mouse/20210126/1000 is a directory.
What is the recommended action I should take to resolve this?

Can't get Crontab jobs to work at all [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed 4 years ago.
New to Crontab use, I am trying to get a simple bash script to run.
add_temp:
#!/bin/bash
rnd1=$RANDOM
range1=20
let "rnd1 %= $range1"
rnd2=$RANDOM
range2=50
let "rnd2 %= $range2"
echo $rnd1
echo $rnd2
cd /var/www/html
sqlite3 test.db <<EOF
INSERT INTO temps (date, temp) VALUES ($rnd1, $rnd2 );
EOF
crontab -e:
SHELL:/bin/bash
* * * * * /var/www/html/add_temp
This doesn't seem to run at all. Works fine if run manually with /var/www/html/add_temp.
My guess is that the sqlite3 is not found when the cron daemon run the script.
You could modify the script and provide the full pathname of this command. Use a terminal to display the full pathname of the command:
type sqlite3
The cron daemon which will run the script does not have the exact same environment than the bash login shell used to run manually the script.
The variable that is usually differently set is PATH.
The first step to troubleshoot this situation is to compare the environments and debug the script.
I suggest to insert these lines below, after the very first line of the script
env
set -x
Run the script manually and redirect output, from a terminal:
/var/www/html/add_temp >/var/tmp/output_m.txt 2>&1
Change the crontab line for:
* * * * * /var/www/html/add_temp >/var/tmp/output_c.txt 2>&1
Wait that the cron daemon executes the script and compare the /var/tmp/output_m.txt and /var/tmp/output_c.txt files.
When the script will be fixed, remove the 2 debug lines from the script and restore the crontab original content.

Running command from cron not working with no error [duplicate]

This question already has an answer here:
Jquery element.text() error "is not a function" when I use elements array
(1 answer)
Closed 4 years ago.
I am trying to create a method to change my desktop background randomly. I am using crontab to handle the change every 10 minutes.
The crontab
*/10 * * * * /usr/bin/feh --recursive --randomize --bg-fill
/home/aaron/Pictures/wallpapers/minimalist 2>&1
The syslog
syslog:Oct 20 09:20:01 skull-nuc CRON[19895]: (aaron) CMD (/usr/bin/feh --recursive --randomize --bg-fill /home/aaron/Pictures/wallpapers/minimalist 2>&1)
syslog:Oct 20 09:30:01 skull-nuc CRON[20449]: (aaron) CMD (/usr/bin/feh --recursive --randomize --bg-fill /home/aaron/Pictures/wallpapers/minimalist 2>&1)
Trouble shooting -
First I changed my shell to sh and tested the command. It works. I tested the command in bash. It works. I allow it to run from cron and nothing happens and no error is produced. It just runs every ten minutes and my background only changes when I do it manually.
I have verified
Script works alone
Script works from sh
cron service is running
cron is running the command with no discernable output
I am unsure what else to do
The cron environment will usually differ from the environment you have in an interactive shell. In this case, you should check the DISPLAY environment variable, which many X utilities use to figure out which session to connect to.
If it's not set, feh will probably fail in just the way you described.
Missing environment variables can be set directly in the command line you're using in the crontab, or you can write a wrapper script that sets up the environment, then calls feh, and then call the wrapper from cron.

How to write a script in python that can open a new linux terminal and do source automatically [duplicate]

This question already has answers here:
how to open a gnome terminal to execute a command with gnome-terminal, constantly?
(2 answers)
Closed 5 years ago.
I need to write a scirpt in python (or bash) which:
open a new linux terminal with a few tabs
do a 'source myfile.csh' command on each of the tab
I've tried with something like this:
os.system("gnome-terminal --tab --working-directory=/repo/ -e 'source myfile.csh' ")
but it didn't work (for example file was not found even though I was in a correct directory or can't open a .csh file blah blah blah)
I've also tried a few other options, but I didn't find a proper soultion for my problem.
I don't know why I can't do a simple "open a new terminal, write a command and execute it"
Maybe is there any simple solution WITHOUT installing any new software (I don't have a root)?
-e will execute a process. However source is an instruction of your shell. What you need to do is to call your shell with -e and pass arguments to the shell in order to execute the source instruction. Should be something like
os.system("gnome-terminal --tab --working-directory=/repo/ -e 'tcsh -c \"source myfile.csh\"'")

Difference between ./script-name.sh and sh script-name.sh while running a bash script? [duplicate]

This question already has answers here:
What is the difference between `./example.sh` and `sh example.sh`
(3 answers)
Closed 7 years ago.
What's the difference b/w running a script using ./script-name and running it using sh scriptname.sh ? I have this script which runs ok when I run it using ./script.sh but doesn't when I run it using sh script.sh . What changes I have to make it work?
./script_name require the script to have x permission for the user, and its first line shabang indicate its interpreter. e.g #! /bin/bash
interpreter_name script_name don't require the x permission.

Resources