Tag Info

New answers tagged

0

Just an advice and opinion; Try your script move to /usr/local/sbin or /usr/local/bin or /usr/sbin or /usr/bin or /sbin or /bin or that are known by cron(shown in PATH variable) and just add to crontab as script name.


0

Convert decimal to binary with bash builtin commands (range 0 to 255): # prepare D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}) num1=10 echo ${D2B[$num1]} >register.txt


0

So after setting up SSH keys and NOPASSWD entry in teh sudoers file, you could do something like #!/bin/bash if [[ ${UID} -ne 0 ]] ; then echo "Must run script as root" >&2 exit 1 fi launchctl unload /Library/LaunchDaemons/com.plex.plexconnect.bash.plist #check what is running on port 80 and if the output matches 'python' then print the ...


0

You can do it using the output redirection as below. $ bc -l <<<"obase=2;$num" > register.txt The above command will overwrite any older results. In case if you want to append your results. $ bc -l <<<"obase=2;$num" >> register.txt >> - Redirects output (STDOUT) messages in append mode. > - Redirects output ...


0

Instead of using tee, just use I/O-Redirection of the shell: echo "obase=2;$num1" | bc >>register.txt The >>-statement causes the output to be redirected (> redirect, >> append) to the file. The -a flag of tee causes also an append to the file. You only need tee if you want the output to be shown in the shell AND redirected to a file. ...


0

The tee command is there to split the output, most often used to get text to a file and to the screen. Just leave it out and use output redirection (appending) to file with >>: echo "obase=2;$num1" | bc >> register.txt


2

awk ' function to_seconds(hms, t) { split(hms, t, /:/) return (t[1]*3600 + t[2]*60 + t[3]) } $2 in times {print to_seconds($1) - times[$2], $2} {times[$2] = to_seconds($1)} ' << DATA 19:32:19 4599544 19:32:22 4599544 19:33:07 4599545 19:33:11 4599545 19:33:58 4599546 19:34:01 ...


1

Given the fact your arrays only have one element and that you test a constant variable, your script, as it is written is equivalent to: #!/bin/sh bridge="gbr0" CSIF="eth2" echo Starting service bridge $bridge brctl addbr $bridge || RETVAL=1 brctl stp $bridge on brctl setbridgeprio $bridge 65000 echo Adding CSIF $CSIF on $bridge ifup $CSIF brctl addif ...


-2

This is exactly what you need: ls -Aru | tail -n 1


0

I had this issue too and I solved it by using EOF. This is starting a ssh connection to sshserverhost that executes test.sh with parameter $HOSTNAME on the remote server (sshserverhost). at now + 1 minutes <<EOF ssh -t sshserverhost 'bash -s' <~/test.sh $HOSTNAME EOF


2

You were close, but you should use single quotes, not double quotes: kill_stopped='kill `jobs -p` ' Backticks are expanded inside double quotes, so it was running jobs -p at the time you defined the alias, not when you used it.


1

Try this: alias kill_stopped="kill \$(jobs -p)" and to kill runnings jobs: kill_stopped If there are no running jobs you get a message about the usage of the kill.


0

The following seems to work just fine in my sh : $ myvar="test" $ echo "My var is $myvar." My var is test. There is no need to echo the value of the variable and catch it again in another. Here is a little bit more information on variable assignments. The thing is, OSTYPE is not defined in sh's environment. You'll have to pass it when running your ...


0

No, it doesn't make a difference. #!/usr/bin/sh means "use /usr/bin/sh to interpret this script so the value of your $SHELL makes no difference.


0

As your script is a shell script (/bin/sh), then your PATH entries in .bashrc will not be read as that is for the bash (/bin/bash) interactive shell. To make your PATH entries available to /bin/sh scripts run by a specific user, add the PATH entry to the .profile file in that users home directory. Additionally you could add the full path for each of your ...


0

The two commands produce the same output only for your input but otherwise they are different. For understanding of what is going on we have to know how is the parameter interpreted first by bash and then by grep. Escaping in bash \ is a special character which cancels special meaning of the following character including \ itself. If the following ...


4

will a script have different behaviour depending on what type of shell is executing it. In the sense that bash script.sh and ksh script.sh are likely to behave differently, yes. Commonly, that difference will be that one of them works and one gives an error, but there are a range of options. Many simple scripts will have the same behaviour on common ...


0

You could probably use git branches for this. Set your home dir up in a branch and then create another branch and configure this how you'd like for this other "profile". It would be a matter of switching branch and then you'd have effectively a different looking home dir. Then you'd probably need to source your environment or run another login shell. I ...


4

The output is the same only for your string, but in general those regular expressions do different things. Let's modify your example a little by adding second pattern e,g, (with comas), third e\.g\. (dots), fourth e\,g\, (comas), and -o option to grep to print only matched parts. In the following case . match any char (notice '' around e.g., I will come to ...


3

When you do a grep e\.g\., the shell is consuming the backslash, thus you are doing a grep e.g., which matches. When you do a grep e\\.g\\., the shell is again consuming a slash, and now you are doing a grep e\.\g., which again matches. Now, a backslash to the shell looks like \\. So, when you have \\, the first one is an escape sequence, the second is a ...


8

First, note that the single slash matches too much: $ echo $'eegg \n e.g.' | grep e\.g\. eegg e.g. As far as Bash is concerned, an escaped period is the same as a period. Bash passes on the period to grep. For grep, a period matches anything. Now, consider: $ echo $'eegg \n e.g.' | grep e\\.g\\. e.g. $ echo $'eegg \n e.g.' | grep e\\\.g\\\. e.g. $ ...


0

You may also try using locate utility, e.g.: updatedb -o ~/tmp.db -l0 -U $PWD; cd $(locate -d ~/tmp.db -l 1 -b -r foo-) For more complex regex pattern check info locate.


3

There is a way in bash 4.3+, which probably comes from ksh: echo_idx_array () # array index { local -n array=$1 # add nameref attribute local idx=$2 echo "${array[idx]}" } $ names=(one two three four) $ echo_idx_array names 2 three $ days=([monday]=eggs [tuesday]=bread [sunday]=jam) # associative array $ echo_idx_array days sunday jam ...


3

ffmpeg -i "stream_link" -codec copy -f mpegts - -codec copy -f flv - | myprogram -h 127.0.0.1 -p 12345 -f - | myprogram -h 127.0.0.1 -p 12345 -f - So If I understand correctly, you are tryithisng to combine these 2 commands into one. mpegts format ffmpeg -i "stream_link" -codec copy -f mpegts - | myprogram -h 127.0.0.1 -p 12345 -f - flv format ...


0

Based on the final requirement you don't need cd; you can do the following: find . -type d -name 'foo-*' -exec make -C {} ';' and find . -type d -name 'foo-*' -exec phing -f {}/build.xml ';' The asterisks are handled by find internally, and I believe this is POSIX compatible.


6

Just to add to the other answers, the code uses ! to avoid writing the code like this: if foo ; then # everything that used to follow "fi" in the previous version else echo "blah" exit 1 fi That only works if there is something after the fi in the script, since an empty if clause isn't legal. (A comment doesn't count.) Writing it this way ...


10

It is a boolean operator that equates to the logical not. See man bash: ! expression     True if expression is false. In your example, if not foo, echo blah.


16

! inverts the exit status of the command -- it's part of POSIX shell syntax, it's not part of if. From the POSIX spec: If the reserved word ! does not precede the pipeline, the exit status shall be the exit status of the last command specified in the pipeline. Otherwise, the exit status shall be the logical NOT of the exit status of the last command. ...


3

getopt is perfectly fine with having no short options. But you need to tell it that you have no short options. It's a quirk in the syntax — from the manual: If no -o or --options option is found in the first part, the first parameter of the second part is used as the short options string. That's what's happening in your test: getopt -l long-key -- ...


-1

There are two different concepts: mandatory vs optional arguments, and options vs operands. Options are arguments that start with -, operands are arguments that don't start with - and aren't the argument of an option. Options are identified by their name and can normally be in any order. Operands are identified by their position. So in someprogram -a -z -c ...


0

Does drush mung backticks and vertical bars?  If not, you could use cd `ls | grep foo- | head -n 1` If backticks don't work, but |, $, ( and ) do, then you could change the above to cd $(ls | grep foo- | head -n 1) If | doen't work, but $, ( and ) do, then you could do cd $(myprog) where myprog is a script that you write to determine the directory ...


2

If you're fine with the optional arguments being at the end, you can just do this: foo=$1 bar=$2 baz=${3:-default value} That will store the first two arguments in $foo and $bar. If a third argument was provided, it will be stored in $baz; otherwise it will default to default value. You can also just check if the third variable is empty: if [ -z "$3" ]; ...


4

@artm showed a technique where you double-quote the awk script and escape various characters. Here are 3 other techniques Break out of the single quote to let the shell expand the variable usrpid=$(awk '$1 == "'"$USR"'" {print $2}' file) Pass the shell variable into an awk variable usrpid=$(awk -v usr="$USR" '$1 == usr {print $2}' file) If the ...


1

the "$USR" in the first example isn't expaneded because it occures inside single quoted string '$1 == "$USR" { print $2 }', so this code is looking for a row with the first column being "$USR", not 62. The following should work: usrpid=$(awk "\$1 == \"$USR\" {print \$2}" /home/hu/batchhu/dbscripts_tst2/user-pid.out2) Changes: the awk command line ...


2

Yes, it is. If you want to do two things concurrently, and wait for them both to complete, you can do something like: sh ./stay/get_it_ios.sh & PIDIOS=$! sh ./stay/get_it_mix.sh & PIDMIX=$! wait $PIDIOS wait $PIDMIX Your script will then run both scripts in parallel, and wait for both scripts to complete before continuing.


0

Dunno about getopt but the getopts builtin can be used to handle only long options like this: while getopts :-: o do case "$o$OPTARG" in (-longopt1) process ;; (-longopt2) process ;; esac; done Of course, as is, that doesn't work if the long-options are supposed to have arguments. It can be done, though, but, as I've learned working on this. While I ...


10

A few pieces of documentation will help to explain this. From the POSIX standards document for the shell: The following variables shall affect the execution of the shell: PS1: Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. ... ...


4

The \ character escapes the following (special) character. In this case, it escapes the $, which we usually use to dereference a variable. When the shell evaluates a variable assignment, it first expands the right-hand-side of the expression. Without the \ before $PWD, the shell expands $PWD and assigns the result to PS1. However, with the \, the shell ...


58

It isn't a shebang, it is just a script that gets run by the default shell. The shell executes the first line //usr/bin/env go run $0 $@ ; exit which causes go to be invoked with the name of this file, so the result is that this file is run as a go script and then the shell exits without looking at the rest of the file. But why start with // instead of ...


8

It runs because by default executable file is assumed to be /bin/sh script. I.e. if you didn't specify any particular shell - it is #!/bin/sh. The // is just ignored in paths - you can consider is at as single '/'. So you can consider that you have shell script with first line: /usr/bin/env go run $0 $@ ; exit What does this line do? It runs 'env' with ...


17

The reason is simple, cd is a shell builtin (and shell function in some shells), while echo is both a binary and a shell builtin: $ type -a cd cd is a shell builtin $ type -a echo echo is a shell builtin echo is /bin/echo sudo can't handle shell builtins, but can handle binaries in the $PATH. When you use sudo echo, /bin/echo is found in the $PATH, so ...


1

The issue is more for sudo cd to fail on your OS than sudo echo to succeed. sudo cd /directory is quite a legitimate method to check if a given user, likely root here, is allowed to cd to some directory. That is the reason why all Posix compliant OSes do provide an executable version of cd. So the answer to you question is sudo echo yo works by design ...


3

running which echo gives /bin/echo echo is a plain program, and sudo can "find" it. On a side note, there must be some option in sudoers(5)


0

Jan's answer is good, the environment variable http_proxy is read by many programs, e.g. wget. You can add this export http_proxy="http://@${proxyserver}:${port}" to your ~/.bash_profile. Also yum works, but you can also specify it in /etc/yum.conf. Other programs can be configured similarly (e.g. git in ~/.gitconfig, chromium --proxy-server=host:port). ...


2

You just need to escape the dollar $.: echo \$PATH $PATH Or surround it in single quotes: echo '$PATH' $PATH This will ensure the word is not interpreted by the shell.


0

Yuo need to export the following environment variables: http_proxy='http://user:pass@PROXY_IP:PROXY_PORT/' https_proxy='http://user:pass@PROXY_IP:PROXY_PORT/' ftp_proxy='http://user:pass@PROXY_IP:PROXY_PORT/'


0

If you're only interested in bailing if a particular argument is missing, Parameter Substitution is great: #!/bin/bash # usage-message.sh : ${1?"Usage: $0 ARGUMENT"} # Script exits here if command-line parameter absent, #+ with following error message. # usage-message.sh: 1: Usage: usage-message.sh ARGUMENT


1

If you use the find command you can omit the files that end in the extension .gz like so: $ ls -l total 0 -rw-rw-r--. 1 saml saml 0 Oct 15 22:42 FAIL -rw-rw-r--. 1 saml saml 0 Oct 15 22:42 FAIL.gz $ find . -name "*FAIL*" ! -name "*.gz" ./FAIL You can also filter ls output like so: $ ls *FAIL* | grep -v '.gz' FAIL But it's generally advisable to not ...


1

You can test against the presence of a regex for those extensions: for file in *FAIL*; do [[ ! $file =~ .(bz2|gz) ]] && printf "%s\n" "$file"; done Insert obligatory warning about not parsing ls...


0

As regards variables, I have the impression that you are missing modules & modulefiles [1]. Once you start doing so, it will be easy to create common profiles for various shells (incl. bash & zsh), python, perl and even more environments, all from the convenience of a single module file. It is also possible to define aliases in the same way; ...



Top 50 recent answers are included