sh

Determine if the program is called from a script in Python

橙三吉。 提交于 2019-12-25 18:54:26
问题 doa #!/bin/sh myexe myexe if sys.stdout.isatty(): print 'from a script' else: print 'not from a script' OUTPUT (if i execute doa from terminal): not from a script OUTPUT (if i execute myexe from terminal): not from a script I want it to say ' from a script ' if executed from doa Question: is it possible for myexe to know that it's being executed from a bash script? 回答1: You can use psutil to ask for the name of the process with id the parent process id: import psutil import os ppid = os

Making perl read from index.html file

倾然丶 夕夏残阳落幕 提交于 2019-12-25 18:27:03
问题 I'm trying to use perl as my web server for a specific address:port number, 10.x.x.x:portNumber, while also having it show what's in my index.html file by default. However, perl does not show the contents of index.html when I run 10.x.x.x:portNumber in my browser. How can I get perl to read from a file? This is the code I'm working with for this problem. #!/usr/bin/perl { package MyWebServer; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); my %dispatch = ( '/' => \&resp

Unable to run shell script file using PHP

谁说我不能喝 提交于 2019-12-25 18:19:30
问题 Hi I am unable to run the .sh file using my php code. Files: index.php and .sh files are in the same directory. What I have tried: echo shell_exec('sh shell_file.sh'); //Did not execute echo shell_exec('shell_file.sh'); //Did not execute echo exec('shell_file.sh'); //Did not execute But when I run the shell_file.sh file manually it does execute. 回答1: Try like this: What you need to do is call the file with a program. Call it with bash or sh as suggested in the comment: echo shell_exec('sh

Any way to accept special characters without delimiters in a shell script?

China☆狼群 提交于 2019-12-25 16:58:14
问题 I'm tasked with a making a shell script that swaps 2 strings and then outputs a file. The commands are similar to: sed s/ search_for / replace /g output.txt > temp.dat mv temp.dat output.txt The script works like this: ./myScript var_A var_B output.file Which I got to work fine. The second part does the same thing, but I must treat the following special characters as regular strings: [ ] ^ * + . $ \ - I have a general idea on how I want to tackle this (this may be the wrong way). I want to

Shell script to send email before password expires

故事扮演 提交于 2019-12-25 16:08:31
问题 I need to get notify the unix box users when there password going to expires in number of days for that i have used the below script. #!/bin/sh rcvr1=test1@testvm.localdomain.com rcvr2=test2@testvm.localdomain.com for i in babinlonston lonston babin do # convert current date to seconds currentdate=`date +%s` # find expiration date of user userexp=`chage -l $i |grep 'Password expires' | cut -d: -f2` # convert expiration date to seconds passexp=`date -d “$userexp” +%s` # find the remaining days

Spinner in Shell

别等时光非礼了梦想. 提交于 2019-12-25 08:49:45
问题 I found this for BASH, but I want to do the same thing with shell ( #!/bin/sh ). The twist is to make it a timer also, so like wait 60 seconds for example until it ends. 回答1: Fadi's own solution is helpful (and comes by courtesy of Adam Katz's comment on the linked answer), but comes with 2 caveats: The spinner, due to using \r , only works at the beginning of a line. Per POSIX, sleep only supports integral seconds. It may also not be readily obvious where to test for whether the operation is

How do I sed only matched grep line

不问归期 提交于 2019-12-25 08:33:05
问题 I want to replace a variable only if two other variables matches. For Example I have this in my data.txt: Mary Jane:Runs:Pasta Mary Jane:Kicks:Apricot John:Runs:Pasta And I want to replace one variable only on the line which matches with both value. Say I want the first variable to match "Mary Jane" and the second to match "Runs" and then change that particular line from "Runs" to "Sleep", so this should be the result: Mary Jane:Sleeps:Pasta Mary Jane:Kicks:Apricot John:Runs:Pasta But I get

How to execute the command after sudo su

五迷三道 提交于 2019-12-25 08:27:25
问题 I need to execute "monit restart haproxy" command after sudo su. This is my script.sh sudo su monit restart haproxy. Here if i will execute the script.sh file then it will hang and it is not executing the monit restart haproxy command. Any idea. Thanks in advanced. 回答1: The sudo command is expecting you to enter a password which is why it will hang, you can use a pipe so that the password is passed to the command like this: echo <password> | sudo su monit restart haproxy. 回答2: Here comes some

Unix Shell Script get only needed lines from a file content

百般思念 提交于 2019-12-25 07:32:46
问题 EDIT I have a file containing line of strings mediafire.com/?a6o37kfc7m68dri NOTE: that file on media fire is actually "logs.out" i've just renamed it to logs.txt when i uploded it. example Content:(logs.out) BIF in bif_init> login successful BIF in dbInit> INFO: DB status:20,successful BIF in dbInit> INFO: get the fileName need be query DB. Size(0) BIF in dbInit> INFO: get the fileName need be query DB. Size(30) BIF in modInit> INFO: modInit finished BIF in setInputTag> set input tag value,

A shell script to to change a value in a file with a parameter

人走茶凉 提交于 2019-12-25 06:07:19
问题 I am an etl developer, am very new to shell scripting, My file is param.prm [3_go:wf_test:s_test] $Dbconnection=abcd $Dbstring=qwert I need a shell script to to read the above file and change the value of $dbconnection (now abcd) dynamically with the variable I pass , every time I run the script. How can I do that? 回答1: $ sed 's/\($Dbconnection=\).*/\1NewValue/' param.prm [3_go:wf_test:s_test] $Dbconnection=NewValue $Dbstring=qwert To change the file in-place, use the -i option. For GNU sed: