Hot answers tagged

7

I'd try to use bash variable substitution: test) shift docker exec -it $(docker-compose ps -q web) python manage.py test "${@-apps}" ;; Other way is to check $* instead of $1: case $* in bash) ... test) docker exec -it $(docker-compose ps -q web) python manage.py test apps ;; test\ *) ...


7

if the user is allowed to use at command, this is the perfect use for that: $ at 08:00 022116 at> myscript.sh at> <----------- ctrl-d here job 9 at 2016-02-21 08:00 if you get a message like "user blah is not able to run at", ask the syadmin to add this user to at.allow file or remove from at.deny file, depending on how it is used in your ...


5

I'm going to assume that what you've posted is a sample, because it isn't valid XML. If this assumption isn't valid, my answer doesn't hold... but if that is the case, you really need to hit the person who gave you the XML with a rolled up copy of the XML spec, and demand they 'fix it'. But really - awk and regular expressions are not the right tool for the ...


4

With zsh: dirs=(test/{1..10}/{1..15}) n=0 mkdir -p $dirs && for d ($dirs) {:>$d/$(([##36]n%26+10)); ((n++))} The trick above being to use base36 numbers where digits 10 to 35 are expressed with A.. Z. Would be a bit more legible and generalisable with: dirs=(test/{1..10}/{1..15}) n=0 l=({A..Z}) mkdir -p $dirs && for d ($dirs) : > ...


4

Your results don't match mine echo 'some test here' > file.txt grep test file.txt echo $? # returns 0 echo 'something else here' > file.txt grep test file.txt echo $? # returns 1 Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the ...


4

Running : $ echo "$fileSize" | od -c 0000000 1 8 2 5 \r \n 0000006 $ echo "$downloadedSize" | od -c 0000000 1 8 2 5 \n 0000005 shows you in the first case there is a carriage return appended to the value (http-headers have dos line terminations: CRLF) while the second variable is correct. Strip the CR (\r) and your test will work. e.g.: ...


3

You have a file named 1 in the directory the script is running in. As MelBurslan commented, [] has a special meaning for the shell, but it doesn't have much to do with regexes: it just means "a single character taken from any of the characters between the brackets". So when you run echo 15:56:14,965 [,PCC12345678(PSI12345678),,] the shell looks for a ...


3

My crystal ball thinks that maybe your message text contains one of more of < or >. It looks to me like your usage mailCommand=`mail -s info@redearmedia.ca < $email` will not do what you want: this will take the content of $email as a filename (failing somewhat because $email consists of several words), try to read its contents, pu tthose into ...


2

My contribution is a POSIX-compliant ed alternative to Thomas Dickey's AWK; it makes the same assumptions: printf '%s\n' '1;?2016/01/30 14:52:51: ?' '1,.-g//s///' w q | ed filename ed's ability to traverse the file backwards makes short work of this task.


2

This should be as simple as: printf "%g" $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) Or, using perl: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | perl -e 'printf("%g\n", <>)' The format you're requesting seems a bit strange, but these two examples will print a number using scientific notation.


2

The process substitution rawtopng <(tail myfile) fileout creates a pipe (just like tail myfile | rawtopng - fileout), and passes a name for the pipe to the program. On Linux, rawtopng will see a name like /dev/fd/42, and if it queries the file type, it'll be told that it's a pipe. No write to the filesystem is required (this is an anonymous pipe, not a ...


2

You can get string length of the variable by using ${#variable}. And you should use -gt instead of > in the [ ] expression. #!/bin/bash pass=$(cat "$1") if [ ${#pass} -gt 32 ]; then echo "Error: Password length invalid" else echo "okay" fi


2

#!/usr/bin/env bash ((!EUID)) && { # check if the EUID is a zero echo "${0##*/} can not be executed as root " # notify exit 1 # exist with status code 1 } gem install jekyll since you are an admin (normal user) the variable $EUID always greater than zero ! and in root $EUID = 0 ((expression)) The expression is evaluated ...


2

something like this would work well if [ -z "$2" ] then echo "No argument supplied" fi


2

echo /test/{1..10}/{1..15} " " | ( chars=({A..Z}); i=0; while read -d " " dir; do test -z "$dir" && break; echo mkdir -p "$dir"; echo touch "${dir}/${chars[i]}"; ((i++)); i=$((i%26)); done )


2

You can just reset your args however you want using set ... case "$1" in #... test) [ $# -lt 2 ] && set test apps docker exec -it $(docker-compose ps -q web) python manage.py $@ ;; esac


2

#!/bin/bash NOW=$(date +"%s") SOON=$(date +"%s" -d "3:00 PM Sun") INTERVAL=$(($SOON-$NOW)) sleep $INTERVAL GNU date allows you to specify the format of the output, as well as the date to display. So I use the format string "%s" to get the time in seconds since the epoch, and the same for the arbitrary time using the -d paramater. Get the difference, and ...


2

The format seems to be correct (after correction applied posted in the comment above). Are there some special restrictions for having everything in a single line? In case you need to have everything in a single line, I would suggest to change the shell script to avoid Fridays 2-9am, eg #!/bin/bash # THIS CODE IS NOT TESTED # skip on fridays 2-9am # what ...


2

curl prints its status to stderr rather than stdout. To capture stderr in the same file, you need to redirect stderr to stdout by adding 2>&1 AFTER your stdout redirection: curl -T /home/pi/fb/$DATE.jpg ftp://myftpserver --user myuser:mypass >> /home/pi/fb/log.txt 2>&1 For a thought-provoking question that will help you understand ...


2

You could find each file to be processed using find and feed each filename to a script made on purpose to look for a match and print the filename in case of a match; I'd suggest to use a script rather than a one-liner for the added easiness of handling the multi-line string compared to the prompt. That is: find . -name "*.[ch]" -exec /path/to/script {} \; ...


2

Since you're trying to match the first 30 lines of your files you could save the text in a file e.g. ref_file then use diff to compare the reference file with the first 30 lines in each file: find . -name "*.[ch]" -exec ./myscript {} \; -print where ./myscript is #!/bin/sh head -n 30 "$1" | diff - /path/to/ref_file >/dev/null so -print in the first ...


2

Use of tr -d '\n' , removes \n (i.e. newline character) as expected. But you are verifying this with echo command. Echo command itself will put new line character @ end of input string and hence you are unable to see expected output. Check variable with below set of commands. test2=$(echo "$test" | tr -d '\n') printf "%s" $test2 | od -c echo -n $test2 | ...


2

echo is appending \n by default. Use -n to omit [6]root@lab7:~> echo 'lol' |od -c 0000000 l o l \n 0000004 [6]root@lab7:~> echo -n 'lol' |od -c 0000000 l o l 0000003


2

messageTemplate=`cat /home/sites/mailmsg.txt` ... mailCommand=`echo "$messageTemplate One or more sites is down"\! | mail -s info@redearmedia.ca` or email="$messageTemplate One or more sites is down"\! mailCommand=`echo "$email" | mail -s info@redearmedia.ca`


1

while ! mount | grep "on ${volume} type" > /dev/null; do sleep $delay if [ "$delay" -gt 60 ]; then exit fi delay=$((delay+5)) done using /proc/mounts You might consider using /proc/mounts instead of the output of mount (which is just /etc/mtab). while ! grep " ${volume} " /proc/mounts &>/dev/null; do


1

If I have understood correctly, this might be a solution in awk!: /^<record/ { x1=""; while (match($0, "record>$")==0) { x1=x1 $0"\n"; getline; } x1=x1 $0; if (x1 ~ />SEARCH</) { print x1 > "output.txt"; } } This will extract the blocks, record> to \record>, containing the key "SEARCH" ...


1

[ -f $num3 ] Doesn't make sense as you're applying the split+glob operator to the content of $num3. [ -f "$num3" ] Would check whether the $num3 path (absolute if it starts with a /, relative to the current working directory if not) resolves to a file that is of type regular or a symlink to a regular file. If you want to check whether $num3 relative to ...


1

Try this #!/bin/bash printf "\n Please enter a file name " read num3 printf "\n Please enter the path to check " read path2check if find $path2check -name $num3 -print -quit | grep -q '^'; then echo "the file exists!" else echo "the file does not exist!" fi



Only top voted, non community-wiki answers of a minimum length are eligible