bash

setting a local bash variable from an ssh command

社会主义新天地 提交于 2021-02-05 09:46:54
问题 I am running a chain of sshpass > ssh > commands, I would like to store the exit code of one of those commands ran in the remote server in a variable so I can access it back in the local after ssh is done. What I have done so far does not seem to store anything. Help please! My Code: sshpass -p password ssh user@ip "echo \"first command\" ; su -lc \"./root_script.sh\" ; set MYVAR = $? ; rm root_script.sh \" if (( $MYVAR != 0 )) ; then echo "Cannot continue program without root password" exit

Why does if [ …something… ]; then echo “Exit status is $?” always emit 0?

让人想犯罪 __ 提交于 2021-02-05 09:46:34
问题 What is the correct way to output an exit status in bash? As far as I know, the exit status called by $? corresponds to the status of the last command executed. The script being worked on has a few conditional checks on the files fed as arguments, for example, a check on whether any files were named at all or if a file exists or not. So I have conditional statements like this: if [ $# -eq 0 ] ; then echo "No file name(s) given! \nExit status=$?" exit if [ ! -e "$fName" ] ; then echo "$fName

Bash assign output to variable inside here document

我与影子孤独终老i 提交于 2021-02-05 09:46:06
问题 In a bash script, what I need is to ssh to a server and do some commands, what I got so far is like this: outPut="ss" ssh user@$serverAddress << EOF cd /opt/logs outPut="$(ls -l)" echo 'output is ' $outPut EOF echo "${outPut}" But the output from terminal is: output is ss ss The output was assigned to the output from command ls -l , but what it showed is still the original value, which is ss . What's wrong here? 回答1: There are actually two different problems here. First, inside a here

Change file's numbers Bash

眉间皱痕 提交于 2021-02-05 09:46:02
问题 I need to implement a script (duplq.sh) that would rename all the text files existing in the current directory using the command line arguments. So if the command duplq.sh pic 0 3 was executed, it would do the following transformation: pic0.txt will have to be renamed pic3.txt pic1.txt to pic4.txt pic2.txt to pic5.txt pic3.txt to pic6.txt etc… So the first argument is always the name of a file the second and the third always a positive digit. I also need to make sure that when I execute my

Change file's numbers Bash

情到浓时终转凉″ 提交于 2021-02-05 09:44:08
问题 I need to implement a script (duplq.sh) that would rename all the text files existing in the current directory using the command line arguments. So if the command duplq.sh pic 0 3 was executed, it would do the following transformation: pic0.txt will have to be renamed pic3.txt pic1.txt to pic4.txt pic2.txt to pic5.txt pic3.txt to pic6.txt etc… So the first argument is always the name of a file the second and the third always a positive digit. I also need to make sure that when I execute my

Read file as input for a command skipping lines

旧街凉风 提交于 2021-02-05 09:36:55
问题 I'm trying to use the contents of a text file as the input for a command. I know how to read a file just fine. However, when I pass the read line to the command I want to execute, the script starts skipping every other line. Given a plain text file named queue : one two three four This prints out each line as expected: queue=`pwd`/queue while read input; do echo $input done < $queue output: one two three four However, when I pass $input off to the command, every other line is skipped: queue=

How to convert all png files to pdf in Bash?

烈酒焚心 提交于 2021-02-05 09:32:10
问题 for f in find *.png; do convert "$f" "$f".pdf; done This is what I have to find the png files in the directory and convert them to pdf, but I get errors. What is a better way to do this in Bash? convert: unable to open image `find': No such file or directory @ error/blob.c/OpenBlob/2705. convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/504. convert: no images defined `find.pdf' @ error/convert.c/ConvertImageCommand/3257. 回答1: The list of filenames you are

perl bash script don't insert sql query

有些话、适合烂在心里 提交于 2021-02-05 09:30:34
问题 i want to insert a query but not work : my $query1 = $db1->prepare("SELECT host, name, severity FROM XX"); my $query2 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = ?, name = ? WHERE HOST = ?'); $query1->execute; while (my @row = $query1->fetchrow_array) { $query2->execute($row[2]); print "$row[2]\n"; } preparation query 3 my $query3 = $db1->prepare("SELECT host, name, severity FROM XX); preparation query 4 my $query4 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = 6,

How to pass arguments to bash command [duplicate]

我们两清 提交于 2021-02-05 09:28:54
问题 This question already has answers here : How to pass command output as multiple arguments to another command (5 answers) Closed 14 days ago . I try to pass this result in the third command -d argument, But i failed. $ cat asnlookups_domains_ip.txt | awk '{print $1}' | amass intel -whois -d flag needs an argument: -d 回答1: You could try xargs : cat asnlookups_domains_ip.txt | awk '{print $1}' | xargs -n1 amass intel -whois -d It passes the standard input as arguments after your command. 来源:

Wildcard in bash script

最后都变了- 提交于 2021-02-05 09:28:27
问题 I have a bash script to retrieve files from ftp. Now the files have one part a date string in the filename, but also undefined numbers that changes on every file. I want to download the files based on the date. This is my code. I only need to do the wildcard trick, the ftp script is allready work. filename=$(echo $TIMESTAMP'0***vel.radar.h5') The stars are 3 digits with different numbers that i can't estimate, so i would use the wildcard for them. Thank you 回答1: It sounds like you want to