Tag Info

Hot answers tagged

7

Your script is attempting to combine two interpreters. You have both #!/bin/bash and #!/usr/bin/expect. That won't work. You can only use one of the two. Since bash was first, your script is being run as a bash script. However, within your script, you have expect commands such as spawn and send. Since the script is being read by bash and not by expect, ...


5

I guess that, in bash anyway, you can use declare and the -global switch to force global scope. Like: total=5 fn(){ local -i total=0 declare -g total=10 echo "$total" };fn echo "$total" ...which prints... 0 10


4

When you run > /tmp/foo.txt, you are overwriting the contents of /tmp/foo.txt with the output of sed 's/old text/new text/' file1.txt. Since /tmp/foo.txt doesn't exist when you run this command, bash will create that file for you and then write it. Then, when you use the -p flag to cp, you are copying the permissions and attributes of /tmp/foo.txt to ...


4

Your code can be a lot more concise: #!/bin/bash read -p "Enter file name: " filePath if ! [[ -r $filePath ]]; then echo "cannot read $filePath" exit 1 fi PS3="Where you want to copy? " select host in host1 host2 host3; do if [[ -n $host ]]; then expect <<END spawn scp "$filePath" uname@$host:/usr/tmp ...


3

With zsh: setopt extendedglob # best in ~/.zshrc extract_numbers() REPLY=${(SM)REPLY##<->-<->} for file (*<->-<->*.dat(no+extract_numbers)) { do-what-you-will-with $file } We define a sorting function (extract_numbers) that returns the Substring of the file name that Matches <->-<-> (<-> matching any positive ...


3

look for "history" related commands in the man page for your shell. most shells can write this to a file or set up a repeating prompt command to do it.


3

This is a likely job for xargs: printf %s\\n '#!/bin/sh' 'printf "<%s>\n" "$$" "$@"' >avg_hourly.sh chmod +x ./avg_hourly.sh xargs -n2 ./avg_hourly.sh <<\IN 2005 01 2005 02 2005 03 IN xargs will split on the spaces by default and invoke the specified command once per -n2 occurring arguments. I just wrote a little dummy avg_hourly.sh script ...


3

It appears you're trying to use ttyS0 as a means to connect two processes. This won't work reliably since ttyS0 is the interface to a serial line (COM1: in Windows-speak). On the other hand, it might be that information is missing from your question. If you really do have a device on your serial port, please make that clear. What I believe you're looking ...


3

As one who has worked in a multitude of ?nix environments, I have had to write in a wide variety of shells. Believe it or not, across platforms, the shells are not the same. So if you maintain your personal library in multiple shells (when necessary) it is very helpful to use extensions to ID the shells. That way when you move to another platform and the ...


3

The error is because you are redirecting all output to $logfile so there is no output for yad to process. The tool you're looking for is tee: NAME tee - read from standard input and write to standard output and files SYNOPSIS tee [OPTION]... [FILE]... DESCRIPTION Copy standard input to each FILE, and also to standard output. So, you ...


3

awk -F'|' -v p='Field3' -v r='T' ' NR==1{ for (i=1;i<=NF;i++) if ($i==p) f=i print next} $f=r ' If you like to format it to use like script.awk save it like: #!/usr/bin/awk -f BEGIN{ OFS=FS="|"} NR==1{ for (i=1;i<=NF;i++) if ($i==p) f=i print next} $f=r ...


3

It's not Unix vs. Linux, but rather BSD vs. GNU. Both families are available as source code and can be installed on either OS. Just googling "GNU for Mac" found several pages with instructions on using Homebrew, or other OSS package manager for mac, to replace the BSD tools with their GNU version.


2

There's no low-level way to swap files, so you need to use an intermediate temporary name. For robustness, make sure that the temporary name won't be used by any other program (so use mktemp) and that it's on the same filesystem as one of the files (otherwise the files would be needlessly copied instead of being just renamed). swap_files () { ...


2

With bash, you can use the -n parameter to the built-in read function to limit the number of characters read without requiring a newline: #!/bin/bash echo "Ready? [Y/n]: " read -n 1 y_or_n echo case "$y_or_n" in [Yy]|"") echo "you said yes" ;; *) echo "you said no" ;; esac This works whether bash is invoked as sh ...


2

Just loop over all lines in the file: while read line do echo "function $line() { return stuff; }" > "$line/function.js" done < foo.txt Of course this assumes you have already directories named as lines in the foo.txt. If this is not the case then first create them with mkdir "$line". Another approach, with awk instead of loop would be: awk ...


2

It's because all of your logic depends on one of $OPT_[AB] being null. But even if you don't pass a -[ab] $OPTARG parameter, you're still setting them at the top of the script with OPT_[AB]=[AB]. So your logic chains never get past the root... if [[ -z $OPT_A ]]; then... ...statement. Well... not all of your logic depends on that. You're also doing: ...


2

cropped_2-a.png was renamed to cropped_2.png. This replaced the original cropped_2.png. Then 2 lines later this file was renamed to cropped_4.png. The best way to avoid this problem is to move the files into a different, initially empty, directory when you're renumbering them, so you don't have to worry about any of the new names conflicting with old names. ...


2

Shell variable $3 is undefined. You seem to be assuming it is the awk variable $3. And you use wrong syntax (quoting!) in the shell test operation. Do everything in shell, or do everything in awk. In awk... echo "Memory Space Details" free -t -m | awk ' /Total/ { print "Total Memory space : "$2 " MB" print "Used Memory Space : "$3" MB" ...


2

lastUpdate="$(stat -c %Y myLogFile.log)" now="$(date +%s)" let diff="${now}-${lastUpdate}"


2

you almost make it awk '(NR>1) && ($8 > 2 ) ' foo > bar where NR is number of record (that is number of line) $8 is eight field && is logical and foo is the original file, unchanged bar resulting file implicit default action is to print the current input line note that header is striped from foo to bar, to keep it awk ...


2

The following works for me in mksh: $ echo $KSH_VERSION @(#)MIRBSD KSH R50 2014/10/07 $ x="Red,Yellow is a color,Blue" $ oIFS=$IFS $ IFS=, $ y=($x) $ IFS=$oIFS $ echo ${y[1]} Yellow is a color I believe it should work the same way in all versions of ksh.


1

You can just do a simple sort. Assuming your file names contain no whitespace or other strange characters, this command will always give you the oldest file: find . -name '*dat' | sort -r | tail -n1 If you need to deal with strange characters, use find . -name '*dat' -print0 | sort -zr | tr '\0' '\n' | tail -n1 So, you could do something along the ...


1

assuming all files do not begin with Sample numeric field are sperated by _ you can't relay on file time I would use sort -t_ -k2n which yield for the four files given in sample Sample_0000137294-003_xyz.dat Sample_0000237826-001_xyz.dat Sample_0000237826-002_xyz.dat Sample_0000437564-001_xyz.dat


1

#!/bin/bash ls /tmp/*dat > filelist while read fn do #do something with /tmp/$fn done < filelist


1

This will run the awk command on the *.csv files in each subdirectory, leaving behind a {dirname}_appended.dat file in that directory containing the output: for f in */ do awk 'FNR > 1' "$f"/*.csv | sort -sk 1,2 | sort -sk 3,3 >"${f}${f%/}_appended.dat" done Discussion Consider: for f in */ ; do awk 'FNR > 1' *.csv | sort -sk 1,2 | sort -sk ...


1

With gnu tar you could use: --transform=EXPRESSION, --xform=EXPRESSION Use sed replace EXPRESSION to transform file names. which means --xform='s|path/to/||' would remove path/to/ from the path; and --exclude=PATTERN Exclude files matching PATTERN, a glob(3)-style wildcard pattern. so you could run: tar -zcvf /path/to/compressed/"$(date ...


1

Just cd to the folder from where you want the tarball tress structure to start, and use relative paths! So for instance: # Note, one folder above cd /path/to/compress/.. # Tell tar to compress the relative path tar -zcvf /path/to/compressed/file.tar.gz compress When you uncompress it, it will create the compress folder in the current working ...


1

Since the awk body is in single quotes, awk does not see the shell variable. awk thinks you mean an awk variable i. That variable is undefined, so in a numeric context it has the value zero. Thus $i is interpreted by awk as $0, or the whole line. You need to pass the shell variable into awk. for ((i=1; i<=$count; i++)) do abc=$(awk -F "," -v col=$i ...


1

You are using abc=$(awk -F "," '(NR==1){print $($i)}' $file). Here you use single quotes around your awk skript. Therefore variables like $i are not replaced by their value. If i=1, awk will therefore literally see $($i) instead of $1 as given in the second example. You can give variables to awk using the -v option. Maybe awk is not the perfect tool for ...


1

spawn is an expect command. It will not work if your interpreter is /bin/bash.



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