New answers tagged

0

Change your single quotes to double quotes. Instead of: mypath='/test/$input_variable/destination/' Use: mypath="/test/$input_variable/destination/"


0

Your main problem is that you think you're setting a variable with your line mypath='/test/$input_variable/destination/' but it is actually run inside the FTP session. You need to move it above the FTP command. You also check conditions after it that can not be checked there for the same reason.


0

You could start by not instantiating Python on every matching byte (!): #!/usr/bin/awk -f function de() { getline < "/proc/uptime" close("/proc/uptime") return $0 } BEGIN { ec = de() } $0 == "ed" { getline byte1 getline byte2 number = strtonum("0x" substr(byte2, 2, 1)) + 2 data = ":: " de() - ec " ::ED." byte1 "." byte2 while (number--) ...


0

I was skeptical of Stéphane’s answer, however it is possible to abuse $#: $ set `seq 101` $ IFS=0 $ [ $# = 0 ] bash: [: too many arguments This is a contrived example, but the potential does exist.


2

Try this instead: osascript -e 'tell Application "Safari" to activate' osascript -e 'tell application "System Events"' -e 'keystroke "a" using command down' -e 'end tell'


0

If this script is ran as root...then removing the entry from crontab can be done by removing its crontab line from /var/spool/cron/root. (This is what crontab -l and crontab -e use). At the end of your delete commands add the following line (substituting {pattern} for your script name): sed -i '/{pattern}/d' /var/spool/cron/root So if your entry in ...


0

Nice and easy... sed -n "s/test/est/2p" -n don't print anything s/ substitute "test" with "est" /2 but only the second instance p print the results


1

You have misunderstood how hard links work. There is no original. All files are simply hardlinks to an inode. Therefore, hardlinks don't actually link to files, they link to inodes. To illustrate, consider this file: $ touch file $ ls -li file 3282140 -rw-r--r-- 1 terdon terdon 0 May 3 16:27 file As you can see above, file points to the inode 3282140. ...


2

One aspect of your problem is that you should use quotation mark to avoid having problems with the shell commands like &. But this is not the only problem. Many websites refuse to serve you if you are using a bot or program like wget. So you have to change the user agent. Go to: http://www.whatsmyua.com/ This website shows the usergent of your browser ...


0

chattr can inhibit anyone but root from being able to delete a file, even a file stored in the trash bin. As root chattr +i /path/to/safe_rm


0

It is most likely trying to run the commands and failing because the iwconfig executable can't be found - because there's no $PATH variable available. You should always use absolute pathnames in cron scripts and similar and not rely on a $PATH setting that might not be there.


64

The kernel interprets the line starting with #! and uses it to run the script, passing in the script's name; so this ends up running /bin/rm scriptname which deletes the script. As hobbs pointed out in a comment, this means your script is actually an rm script, not a bash script — the latter would start with #!/bin/bash. See How programs get run for ...


1

If you are using readlink -e, that assumes GNU coreutils. But using ls to obtain the inode value is indirect. By using stat (also from the same coreutils package), you can get the inode value directly. If you limit the comparison to inodes, you can also prevent symbolic links which you may have setup to point to your script from being removed. Here is an ...


2

Use public key authentication In the source host run this only once: ssh-keygen -t rsa # ENTER to every field ssh-copy-id myname@somehost That's all, after that you'll be able to do ssh without password. Coming to your question, use below command now, ssh apple@192.168.1.117 'ls -l /applications'


0

You can run the command in a non-login, non-interactive shell session using ssh: ssh apple@192.168.1.117 'ls -l /applications'


2

Use <<-EOF instead of <<EOF when you want to use indentation. Then indent the text by tabs. Finally you need to put EOF to mark the end of the here-document. Your code block will be like: ftp -inv $HOST <<-EOF user $USER $PASSWORD cd /work/test//$input_variable/path/to destination/ mput x.csv ...


0

#!/bin/bash set -x # prepare test data. mkdir -p ~/test_var_global cd ~/test_var_global echo "a"> core.1 echo "b"> core.2 echo "c"> core.3 var=0 coreFiles=$(find . -type f -name "core*") while read -r file; do # perform computations on $i ((var++)) done <<EOF $coreFiles EOF echo $var Result: ... + echo 3 3 it can work.


2

You are using printf to escape the spaces, as well as quoting it "" If you omit the printf call, and use the original $d with double-quotes, you are good. f=`echo $d | tr ' ' '_' | tr -d ',.!'` # Clean the name t=$YEAR_DIR/$f.tar.gz ##d=$(printf '%q' "$d") escapes the spaces # echo tar czf $t "$d" # This outputs the command as expected ...


3

Here's an awk script that stores the whole file in memory: awk '{line[NR]=$0} END {for (i=NR; i>=1; i--) print line[i]}' file Phrased as a shell function: tac () { awk '{line[NR]=$0} END {for (i=NR; i>=1; i--) print line[i]}' "$@"; }


1

Here's a clean clear-cut POSIX solution for on-disk files: #!/bin/sh function tac () { lines=$(wc -l < "$1") while [ $lines -gt 0 ] do head -n $lines "$1" | tail -n 1 lines=$((lines-1)) done } The main down-side is that it reads the file once for every line in the file. POSIX doesn't specify an upper limit for -n number, so large files ...


2

You can do this with a sed one-liner as follows, though it is certainly not readable for the "uninitiated": sed -n '1h;1!{x;H;};${g;p;}' file.txt Explanation: -n suppresses sed's default action of printing each line. 1h causes the first line to be stored in the hold space. 1!{...} applies this block of commands to all lines except for the first one. ...


2

See if perl is portable enough: perl -MTime::HiRes=time -MPOSIX=strftime -e ' $now = int(time() * 1000); printf "%s.%03d\n", strftime("%H:%M:%S", localtime(int($now/1000))), ($now % 1000); ' Time::HiRes and POSIX are both core Perl modules. If you just want the epoch timestamp with fractional seconds: $ perl -MTime::HiRes=time -le 'print time()' ...


1

You're just one step short of a working solution. Canonicalize the variable, and you're done: elif [ "$(readlink -e "$1")" = "$(readlink -e ~/project/safe_rm)" ] ; I wrap everything into quotes because paths may contain spaces. Note that you don't have to escape the quotes inside subshell ($(…) thingy). Thanks to @glenn jackman for this correction.


3

When you run this command: wget https://translate.google.hu/?hl=hu&tab=wT#en/hu/Enthusiast what really happens is: you run wget with URL of "https://translate.google.hu/?hl=hu"; ampersand means that wget will run in background; a variable named tab is defined and gets a value wT#en/hu/Enthusiast. The reason for all this is that shell reserves some ...


-1

Try: if [ -e filename ]; then # something fi with just one square bracket


1

The test you are looking for is: [[ -f /path/to/file ]] where /path/to/file is the path to the file you are testing to see if it exists. You can place this test in a conditional using (provided DEST_DIR is the destination directory for the file SRC_FILE): if ! [[ -f DEST_DIR/file ]]; then mv SRC_FILE DEST_DIR fi where ! returns true is the file does ...


4

This will give you how many miliseconds have passed since the beginning of the Unix epoch : date +%s%3N You have to reduce the seconds*1000 which have passed until 00:00 AM today. Or you can just convert everything into miliseconds from this formula : date +%H%M%S%3N Like : h=$(date +%H) m=$(date +%M) s=$(date +%S) ms=$(date +%3N) echo ...


3

For terminals that support it, you could use the save cursor and restore cursor escape sequences: #! /bin/sh - save_cursor=$(tput sc) restore_cursor=$(tput rc) text="\ XX----------------------------------------------------------XX |XXX |XXX | XX | ...


3

If you have ncurses installed, you can use the tput command to move the cursor to some place on the terminal, where you can use any printing command. Example: clear for i in $(seq 1 40) do echo '## ##' done for i in $(seq 1 100) do tput cup 20 5 date sleep 1 done You may ...


1

Maybe something like this solves the issue: #!/bin/bash ...


2

id no should be of 6 characters (like M000001). Something like this might do that: if [[ ${#input_variable} != "6" ]] then echo "wrong length" exit 1 fi When I enter the correct id, the script is copying the file. However when i enter wrong id eg. M000050 which is not present in remote server, then also it is showing the file is ...


0

You can certainly use Toad software which is a freeware and it has excellent options available to compare the databases, tables and schema. Check here https://www.toadworld.com/products/toad-for-sql-server I found this very handy.


0

To compare the differences between schemas of two mysql databases from the command line you can use mysqldiff example usage from the documentation shell> mysqldiff --server1=root@localhost \ employees.salaries:emp1.salaries --differ # server1 on localhost: ... connected. # Comparing employees.salaries to emp1.salaries ...


0

If words are a list where values are separated by a newline, you can do: WORDS="$(ls -1)" if echo "${WORDS}" | grep --quiet --line-regexp --fixed-strings "${WORD}"; then echo yes else echo no fi


1

myfile="My File [Official Awesome File].txt" myfile=$(echo $myfile | sed -r 's/(\[|\])/\\\1/g') find ./ -name "${myfile}" -type f can you check if this works? I'm expecting this'll change value of myfile to myfile="My File \[Official Awesome File\].txt" Better would be to use printf: myfile=$(printf '%q\n' "$myfile") In your specific example ...


0

echo -ne "HTTP/1.1 401 Unauthorized\nWWW-Authenticate: Basic realm="Restricted"\r\n\r\n`date`" | nc -l -p 8080 Output: GET / HTTP/1.1 Host: 127.0.0.1:8080 User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en-US,en;q=0.5 ...


5

ShellCheck is a good start for bash programming. It gives quite useful hints: Line 6: if [[ "$CDTRACK" =~ "([[:alpha:][:blank:]]*)- ([[:digit:]]*) - (.*)$" ]] ^-- SC2076: Don't quote rhs of =~, it'll match literally rather than as a regex. Regex can't be quoted like this. Working example with escaped special characters (basically ...


2

As Stephen Kitt pointed out, the following only works correctly if there's zero or one matching directories. If there's more, the glob will match them all, and you'll end up moving the file and all but the last directory into the last directory that matched. Take care. When shell sees an asterisk (or a question mark), it tries to find a path matching the ...


0

"Authorization required" popup is generated when the server returns HTTP code 401 and the browser doesn't have the credentials yet. There are two modes of authorization: basic and digest. In basic mode, the password is transfered in the clear and is readable by everyone; in digest mode, only the hashes of credentials are transferred, so there's much less ...


1

The two main reasons to run a program directly without calling the shell are: Performance: Most programs that you would call from your C program are likely much smaller than the shell, which makes them start much more quickly. Environment control: Dealing with an additional layer of environment variables to deal with can be more complex to configure and ...


0

$ FLAG=$(awk -F\| '{printf("%s, ", $1)}' file1.txt) $ echo $FLAG 1234, 1345, 8427, 2132, 3243, $ sed "s/FLAG/${FLAG%%, }/" select.sql SELECT * FROM CUSTOMERS WHERE ID IN (1234, 1345, 8427, 2132, 3243); This depends on the flag list being small enough to fit on the command line. If it's not, you can use getline in awk to process file1.txt, gathering ...


1

You can use... echo password | sudo -S recover.sh Password being your sudo password. From sudo manpage.. -S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. And second method is sudo -S <<< password apt-get install pkg_name


0

The short answer is, ssh srv_1 'ssh srv_2 "command"' where command is whatever command you want to run. It also works without the single quotes, but I like them to aid in understanding. ssh srv_1 ssh srv_2 "command" Using the quotes gives us some structure to work within, but as you can see, they're not fundamentally necessary. This also works, ssh ...


1

If you don't want to enter password manually you use -A option of sudo -A, --askpass Normally, if sudo requires a password, it will read it from the user's terminal. If the -A (askpass) option is specified, a (possibly graphi‐ cal) helper program is executed to read the user's password and output the ...


1

Ok, this will need some work, but to start check what are the adapters with ifconfig Then run: wl -i ethX assoclist until you find the one that ddwrt is using for your wifi. You still have to mess a bit to get your exact output structure but the original script (plus -i adapter) should work.


0

#! /bin/ksh inputfile='file1.txt' sqlfile='select.sql' S_ids=$(awk -F"|" '{gsub(/^|$/,"\\'\''",$1) ; print $1","}' "$inputfile" | xargs | sed -e 's/,$//') sed -i "s/FLAG/${S_ids}/g" "$sqlfile" Output: $ cat select.sql SELECT * FROM CUSTOMERS WHERE ID IN ('1234', '1345', '8427', '2132', '3243'); This uses awk's gsub() function ...


1

With recent Linux, printf foo > /proc/$$/comm will change the executable name (the ps -p thing) provided "noclobber" isn't set (and the wind is in the right direction). In zsh, printf foo >! /proc/$$/comm works regardless of clobbering state.


0

I think your error is in not having a do done block after your for statement.


7

The syntax for a for loop is like this: for x in {list} do command... command... done There is neither do nor done on any of your loops. Bash Guide for Beginners and Advanced Bash Scripting Guide are good references on syntax and best scripting practices.


0

This had me thinking. The problem with sed/awk/tail is that they're line by line. After you delete the first line you have to write every other line from pattern space to the file. You can use the following commands to do what you want in seconds. This will write the entire file to an array. Remove the first line as it dumps it back into the file. ...



Top 50 recent answers are included